Cutelyst  2.5.0
memcached.h
1 /*
2  * Copyright (C) 2017 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef CUTELYSTMEMCACHED_H
20 #define CUTELYSTMEMCACHED_H
21 
22 #include <Cutelyst/cutelyst_global.h>
23 #include <Cutelyst/plugin.h>
24 
25 #include <QDataStream>
26 
27 namespace Cutelyst {
28 
29 class Context;
30 class MemcachedPrivate;
31 
141 class CUTELYST_PLUGIN_MEMCACHED_EXPORT Memcached : public Plugin
142 {
143  Q_OBJECT
144  Q_DECLARE_PRIVATE(Memcached)
145 public:
149  Memcached(Application *parent);
150 
154  virtual ~Memcached();
155 
180  End,
201  InProgress,
202  ServerTemporaryDisabled,
203  ServerMemoryAllocationFailure,
204  MaximumReturn,
205  PluginNotRegisterd
206  };
207  Q_ENUM(MemcachedReturnType)
208 
209 
213  void setDefaultConfig(const QVariantMap &defaultConfig);
214 
226  static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
227 
241  template< typename T>
242  static bool set(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
243 
259  static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
260 
277  template< typename T>
278  static bool setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
279 
294  static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
295 
312  template< typename T>
313  static bool add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
314 
333  static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
334 
355  template< typename T>
356  static bool addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
357 
372  static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
373 
390  template< typename T>
391  static bool replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
392 
411  static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
412 
433  template< typename T>
434  static bool replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
435 
450  static QByteArray get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
451 
480  template< typename T>
481  static T get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
482 
501  static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
502 
535  template< typename T>
536  static T getByKey(const QString &groupKey, const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
537 
545  static bool remove(const QString &key, MemcachedReturnType *returnType = nullptr);
546 
558  static bool removeByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType = nullptr);
559 
566  static bool exist(const QString &key, MemcachedReturnType *returnType = nullptr);
567 
578  static bool existByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType = nullptr);
579 
584  static const time_t expirationNotAdd;
585 
600  static bool increment(const QString &key, uint32_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
601 
619  static bool incrementByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
620 
643  static bool incrementWithInitial(const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
644 
672  static bool incrementWithInitialByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
673 
688  static bool decrement(const QString &key, uint32_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
689 
707  static bool decrementByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
708 
731  static bool decrementWithInitial(const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
732 
760  static bool decrementWithInitialByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
761 
773  static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
774 
788  template< typename T>
789  static bool cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
790 
807  static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
808 
827  template< typename T>
828  static bool casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
829 
837  static bool flushBuffers(MemcachedReturnType *returnType = nullptr);
838 
849  static bool flush(time_t expiration, MemcachedReturnType *returnType = nullptr);
850 
863  static QHash<QString, QByteArray> mget(const QStringList &keys, QHash<QString, uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
864 
879  template< typename T>
880  static QHash<QString, T> mget(const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
881 
899  static QHash<QString, QByteArray> mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
900 
920  template< typename T>
921  static QHash<QString, T> mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
922 
931  static bool touch(const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
932 
946  static bool touchByKey(const QString &groupKey, const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
947 
951  static QString errorString(Context *c, MemcachedReturnType rt);
952 
953 protected:
954  const QScopedPointer<MemcachedPrivate> d_ptr;
955 
959  virtual bool setup(Application *app) override;
960 
961 private:
962  Q_PRIVATE_SLOT(d_func(), void _q_postFork(Application*))
963 };
964 
965 template< typename T>
966 bool Memcached::set(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
967 {
968  QByteArray data;
969  QDataStream out(&data, QIODevice::WriteOnly);
970  out << value;
971  return Memcached::set(key, data, expiration, returnType);
972 }
973 
974 template< typename T>
975 bool Memcached::setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
976 {
977  QByteArray data;
978  QDataStream out(&data, QIODevice::WriteOnly);
979  out << value;
980  return Memcached::setByKey(groupKey, key, data, expiration, returnType);
981 }
982 
983 template< typename T>
984 bool Memcached::add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
985 {
986  QByteArray data;
987  QDataStream out(&data, QIODevice::WriteOnly);
988  out << value;
989  return Memcached::add(key, data, expiration, returnType);
990 }
991 
992 template< typename T>
993 bool Memcached::addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
994 {
995  QByteArray data;
996  QDataStream out(&data, QIODevice::WriteOnly);
997  out << value;
998  return Memcached::addByKey(groupKey, key, data, expiration, returnType);
999 }
1000 
1001 template< typename T>
1002 bool Memcached::replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1003 {
1004  QByteArray data;
1005  QDataStream out(&data, QIODevice::WriteOnly);
1006  out << value;
1007  return Memcached::replace(key, data, expiration, returnType);
1008 }
1009 
1010 template< typename T>
1011 bool Memcached::replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1012 {
1013  QByteArray data;
1014  QDataStream out(&data, QIODevice::WriteOnly);
1015  out << value;
1016  return Memcached::replaceByKey(groupKey, key, data, expiration, returnType);
1017 }
1018 
1019 template< typename T>
1020 T Memcached::get(const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1021 {
1022  T retVal;
1023  QByteArray ba = Memcached::get(key, cas, returnType);
1024  if (!ba.isEmpty()) {
1025  QDataStream in(&ba, QIODevice::ReadOnly);
1026  in >> retVal;
1027  }
1028  return retVal;
1029 }
1030 
1031 template< typename T>
1032 T Memcached::getByKey(const QString &groupKey, const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1033 {
1034  T retVal;
1035  QByteArray ba = Memcached::getByKey(groupKey, key, cas, returnType);
1036  if (!ba.isEmpty()) {
1037  QDataStream in(&ba, QIODevice::ReadOnly);
1038  in >> retVal;
1039  }
1040  return retVal;
1041 }
1042 
1043 template< typename T>
1044 bool Memcached::cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1045 {
1046  QByteArray data;
1047  QDataStream out(&data, QIODevice::WriteOnly);
1048  out << value;
1049  return Memcached::cas(key, data, expiration, cas, returnType);
1050 }
1051 
1052 template< typename T>
1053 bool Memcached::casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1054 {
1055  QByteArray data;
1056  QDataStream out(&data, QIODevice::WriteOnly);
1057  out << value;
1058  return Memcached::casByKey(groupKey, key, data, expiration, cas, returnType);
1059 }
1060 
1061 template< typename T>
1062 QHash<QString, T> Memcached::mget(const QStringList &keys, QHash<QString, uint64_t> *casValues, MemcachedReturnType *returnType)
1063 {
1064  QHash<QString, T> hash;
1065  QHash<QString,QByteArray> _data = Memcached::mget(keys, casValues, returnType);
1066  if (!_data.empty()) {
1067  auto i = _data.constBegin();
1068  while (i != _data.constEnd()) {
1069  T retVal;
1070  QDataStream in(i.value());
1071  in >> retVal;
1072  hash.insert(i.key(), retVal);
1073  ++i;
1074  }
1075  }
1076  return hash;
1077 }
1078 
1079 template< typename T>
1080 QHash<QString, T> Memcached::mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues, MemcachedReturnType *returnType)
1081 {
1082  QHash<QString, T> hash;
1083  QHash<QString,QByteArray> _data = Memcached::mgetByKey(groupKey, keys, casValues, returnType);
1084  if (!_data.empty()) {
1085  auto i = _data.constBegin();
1086  while (i != _data.constEnd()) {
1087  T retVal;
1088  QDataStream in(i.value());
1089  in >> retVal;
1090  hash.insert(i.key(), retVal);
1091  ++i;
1092  }
1093  }
1094  return hash;
1095 }
1096 
1097 }
1098 
1099 #endif // CUTELYSTMEMCACHED_H
static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:364
static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:406
static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:445
static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1000
static QHash< QString, QByteArray > mgetByKey(const QString &groupKey, const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1164
static const time_t expirationNotAdd
Definition: memcached.h:584
static QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:487
static QHash< QString, QByteArray > mget(const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1089
The Cutelyst Context.
Definition: context.h:50
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:283
static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:960
The Cutelyst Application.
Definition: application.h:55
static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:540
Cutelyst Memcached plugin.
Definition: memcached.h:141
static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:325
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:244