Cutelyst  3.1.0
memcached.h
1 /*
2  * Copyright (C) 2017-2018 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 #include <QVersionNumber>
27 
28 namespace Cutelyst {
29 
30 class Context;
31 class MemcachedPrivate;
32 
142 class CUTELYST_PLUGIN_MEMCACHED_EXPORT Memcached : public Plugin
143 {
144  Q_OBJECT
145  Q_DECLARE_PRIVATE(Memcached)
146 public:
150  Memcached(Application *parent);
151 
155  virtual ~Memcached() override;
156 
181  End,
202  InProgress,
203  ServerTemporaryDisabled,
204  ServerMemoryAllocationFailure,
205  MaximumReturn,
206  PluginNotRegisterd
207  };
208  Q_ENUM(MemcachedReturnType)
209 
210 
214  void setDefaultConfig(const QVariantMap &defaultConfig);
215 
227  static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
228 
242  template< typename T>
243  static bool set(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
244 
260  static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
261 
278  template< typename T>
279  static bool setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
280 
295  static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
296 
313  template< typename T>
314  static bool add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
315 
334  static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
335 
356  template< typename T>
357  static bool addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
358 
373  static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
374 
391  template< typename T>
392  static bool replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
393 
412  static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
413 
434  template< typename T>
435  static bool replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
436 
451  static QByteArray get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
452 
481  template< typename T>
482  static T get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
483 
502  static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
503 
536  template< typename T>
537  static T getByKey(const QString &groupKey, const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
538 
546  static bool remove(const QString &key, MemcachedReturnType *returnType = nullptr);
547 
559  static bool removeByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType = nullptr);
560 
567  static bool exist(const QString &key, MemcachedReturnType *returnType = nullptr);
568 
579  static bool existByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType = nullptr);
580 
585  static const time_t expirationNotAdd;
586 
601  static bool increment(const QString &key, uint32_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
602 
620  static bool incrementByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
621 
644  static bool incrementWithInitial(const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
645 
673  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);
674 
689  static bool decrement(const QString &key, uint32_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
690 
708  static bool decrementByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
709 
732  static bool decrementWithInitial(const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
733 
761  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);
762 
774  static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
775 
789  template< typename T>
790  static bool cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
791 
808  static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
809 
828  template< typename T>
829  static bool casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
830 
838  static bool flushBuffers(MemcachedReturnType *returnType = nullptr);
839 
850  static bool flush(time_t expiration, MemcachedReturnType *returnType = nullptr);
851 
864  static QHash<QString, QByteArray> mget(const QStringList &keys, QHash<QString, uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
865 
880  template< typename T>
881  static QHash<QString, T> mget(const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
882 
900  static QHash<QString, QByteArray> mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
901 
921  template< typename T>
922  static QHash<QString, T> mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
923 
932  static bool touch(const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
933 
947  static bool touchByKey(const QString &groupKey, const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
948 
952  static QString errorString(Context *c, MemcachedReturnType rt);
953 
957  static QVersionNumber libMemcachedVersion();
958 
959 protected:
961 
965  virtual bool setup(Application *app) override;
966 };
967 
968 template< typename T>
969 bool Memcached::set(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
970 {
971  QByteArray data;
972 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
973  QDataStream out(&data, QIODevice::WriteOnly);
974 #else
975  QDataStream out(&data, QIODeviceBase::WriteOnly);
976 #endif
977  out << value;
978  return Memcached::set(key, data, expiration, returnType);
979 }
980 
981 template< typename T>
982 bool Memcached::setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
983 {
984  QByteArray data;
985 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
986  QDataStream out(&data, QIODevice::WriteOnly);
987 #else
988  QDataStream out(&data, QIODeviceBase::WriteOnly);
989 #endif
990  out << value;
991  return Memcached::setByKey(groupKey, key, data, expiration, returnType);
992 }
993 
994 template< typename T>
995 bool Memcached::add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
996 {
997  QByteArray data;
998 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
999  QDataStream out(&data, QIODevice::WriteOnly);
1000 #else
1001  QDataStream out(&data, QIODeviceBase::WriteOnly);
1002 #endif
1003  out << value;
1004  return Memcached::add(key, data, expiration, returnType);
1005 }
1006 
1007 template< typename T>
1008 bool Memcached::addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1009 {
1010  QByteArray data;
1011 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1012  QDataStream out(&data, QIODevice::WriteOnly);
1013 #else
1014  QDataStream out(&data, QIODeviceBase::WriteOnly);
1015 #endif
1016  out << value;
1017  return Memcached::addByKey(groupKey, key, data, expiration, returnType);
1018 }
1019 
1020 template< typename T>
1021 bool Memcached::replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1022 {
1023  QByteArray data;
1024 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1025  QDataStream out(&data, QIODevice::WriteOnly);
1026 #else
1027  QDataStream out(&data, QIODeviceBase::WriteOnly);
1028 #endif
1029  out << value;
1030  return Memcached::replace(key, data, expiration, returnType);
1031 }
1032 
1033 template< typename T>
1034 bool Memcached::replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1035 {
1036  QByteArray data;
1037 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1038  QDataStream out(&data, QIODevice::WriteOnly);
1039 #else
1040  QDataStream out(&data, QIODeviceBase::WriteOnly);
1041 #endif
1042  out << value;
1043  return Memcached::replaceByKey(groupKey, key, data, expiration, returnType);
1044 }
1045 
1046 template< typename T>
1047 T Memcached::get(const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1048 {
1049  T retVal;
1050  QByteArray ba = Memcached::get(key, cas, returnType);
1051  if (!ba.isEmpty()) {
1052 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1054 #else
1055  QDataStream in(&ba, QIODeviceBase::ReadOnly);
1056 #endif
1057  in >> retVal;
1058  }
1059  return retVal;
1060 }
1061 
1062 template< typename T>
1063 T Memcached::getByKey(const QString &groupKey, const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1064 {
1065  T retVal;
1066  QByteArray ba = Memcached::getByKey(groupKey, key, cas, returnType);
1067  if (!ba.isEmpty()) {
1068 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1070 #else
1071  QDataStream in(&ba, QIODeviceBase::ReadOnly);
1072 #endif
1073  in >> retVal;
1074  }
1075  return retVal;
1076 }
1077 
1078 template< typename T>
1079 bool Memcached::cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1080 {
1081  QByteArray data;
1082 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1083  QDataStream out(&data, QIODevice::WriteOnly);
1084 #else
1085  QDataStream out(&data, QIODeviceBase::WriteOnly);
1086 #endif
1087  out << value;
1088  return Memcached::cas(key, data, expiration, cas, returnType);
1089 }
1090 
1091 template< typename T>
1092 bool Memcached::casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1093 {
1094  QByteArray data;
1095 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1096  QDataStream out(&data, QIODevice::WriteOnly);
1097 #else
1098  QDataStream out(&data, QIODeviceBase::WriteOnly);
1099 #endif
1100  out << value;
1101  return Memcached::casByKey(groupKey, key, data, expiration, cas, returnType);
1102 }
1103 
1104 template< typename T>
1106 {
1107  QHash<QString, T> hash;
1108  QHash<QString,QByteArray> _data = Memcached::mget(keys, casValues, returnType);
1109  if (!_data.empty()) {
1110  auto i = _data.constBegin();
1111  while (i != _data.constEnd()) {
1112  T retVal;
1113  QDataStream in(i.value());
1114  in >> retVal;
1115  hash.insert(i.key(), retVal);
1116  ++i;
1117  }
1118  }
1119  return hash;
1120 }
1121 
1122 template< typename T>
1124 {
1125  QHash<QString, T> hash;
1126  QHash<QString,QByteArray> _data = Memcached::mgetByKey(groupKey, keys, casValues, returnType);
1127  if (!_data.empty()) {
1128  auto i = _data.constBegin();
1129  while (i != _data.constEnd()) {
1130  T retVal;
1131  QDataStream in(i.value());
1132  in >> retVal;
1133  hash.insert(i.key(), retVal);
1134  ++i;
1135  }
1136  }
1137  return hash;
1138 }
1139 
1140 }
1141 
1142 #endif // CUTELYSTMEMCACHED_H
The Cutelyst Application.
Definition: application.h:56
The Cutelyst Context.
Definition: context.h:52
Cutelyst Memcached plugin.
Definition: memcached.h:143
static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:447
static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:327
static QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:489
static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1004
static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:964
static QHash< QString, QByteArray > mgetByKey(const QString &groupKey, const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1168
static const time_t expirationNotAdd
Definition: memcached.h:585
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:246
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:285
static QHash< QString, QByteArray > mget(const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1093
static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:408
static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:366
static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:542
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
bool isEmpty() const const
QHash::const_iterator constBegin() const const
QHash::const_iterator constEnd() const const
bool empty() const const
QHash::iterator insert(const Key &key, const T &value)