cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
memcached.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef CUTELYSTMEMCACHED_H
7 #define CUTELYSTMEMCACHED_H
8 
9 #include <Cutelyst/cutelyst_global.h>
10 #include <Cutelyst/plugin.h>
11 
12 #include <QDataStream>
13 #include <QVersionNumber>
14 
15 namespace Cutelyst {
16 
17 class Context;
18 class MemcachedPrivate;
19 
157 class CUTELYST_PLUGIN_MEMCACHED_EXPORT Memcached : public Plugin
158 {
159  Q_OBJECT
160  Q_DECLARE_PRIVATE(Memcached)
161 public:
165  Memcached(Application *parent);
166 
170  virtual ~Memcached() override;
171 
182  UnknownReadFailure,
196  SomeErrors,
199  End,
204  FetchNotFinished,
209  InvalidHostProtocol,
213  UnknownStatKey,
221  ParseError,
225  InProgress,
226  ServerTemporaryDisabled,
227  ServerMemoryAllocationFailure,
228  MaximumReturn,
229  PluginNotRegisterd
231  };
232  Q_ENUM(MemcachedReturnType)
233 
234 
238  void setDefaultConfig(const QVariantMap &defaultConfig);
239 
252  static bool set(const QString &key,
253  const QByteArray &value,
254  time_t expiration,
255  MemcachedReturnType *returnType = nullptr);
256 
271  template <typename T>
272  static bool set(const QString &key,
273  const T &value,
274  time_t expiration,
275  MemcachedReturnType *returnType = nullptr);
276 
293  static bool setByKey(const QString &groupKey,
294  const QString &key,
295  const QByteArray &value,
296  time_t expiration,
297  MemcachedReturnType *returnType = nullptr);
298 
316  template <typename T>
317  static bool setByKey(const QString &groupKey,
318  const QString &key,
319  const T &value,
320  time_t expiration,
321  MemcachedReturnType *returnType = nullptr);
322 
338  static bool add(const QString &key,
339  const QByteArray &value,
340  time_t expiration,
341  MemcachedReturnType *returnType = nullptr);
342 
361  template <typename T>
362  static bool add(const QString &key,
363  const T &value,
364  time_t expiration,
365  MemcachedReturnType *returnType = nullptr);
366 
386  static bool addByKey(const QString &groupKey,
387  const QString &key,
388  const QByteArray &value,
389  time_t expiration,
390  MemcachedReturnType *returnType = nullptr);
391 
413  template <typename T>
414  static bool addByKey(const QString &groupKey,
415  const QString &key,
416  const T &value,
417  time_t expiration,
418  MemcachedReturnType *returnType = nullptr);
419 
435  static bool replace(const QString &key,
436  const QByteArray &value,
437  time_t expiration,
438  MemcachedReturnType *returnType = nullptr);
439 
457  template <typename T>
458  static bool replace(const QString &key,
459  const T &value,
460  time_t expiration,
461  MemcachedReturnType *returnType = nullptr);
462 
482  static bool replaceByKey(const QString &groupKey,
483  const QString &key,
484  const QByteArray &value,
485  time_t expiration,
486  MemcachedReturnType *returnType = nullptr);
487 
509  template <typename T>
510  static bool replaceByKey(const QString &groupKey,
511  const QString &key,
512  const T &value,
513  time_t expiration,
514  MemcachedReturnType *returnType = nullptr);
515 
533  static QByteArray
534  get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
535 
566  template <typename T>
567  static T
568  get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
569 
590  static QByteArray getByKey(const QString &groupKey,
591  const QString &key,
592  uint64_t *cas = nullptr,
593  MemcachedReturnType *returnType = nullptr);
594 
630  template <typename T>
631  static T getByKey(const QString &groupKey,
632  const QString &key,
633  uint64_t *cas = nullptr,
634  MemcachedReturnType *returnType = nullptr);
635 
644  static bool remove(const QString &key, MemcachedReturnType *returnType = nullptr);
645 
658  static bool removeByKey(const QString &groupKey,
659  const QString &key,
660  MemcachedReturnType *returnType = nullptr);
661 
669  static bool exist(const QString &key, MemcachedReturnType *returnType = nullptr);
670 
682  static bool existByKey(const QString &groupKey,
683  const QString &key,
684  MemcachedReturnType *returnType = nullptr);
685 
691  static const time_t expirationNotAdd;
692 
708  static bool increment(const QString &key,
709  uint32_t offset,
710  uint64_t *value = nullptr,
711  MemcachedReturnType *returnType = nullptr);
712 
731  static bool incrementByKey(const QString &groupKey,
732  const QString &key,
733  uint64_t offset,
734  uint64_t *value = nullptr,
735  MemcachedReturnType *returnType = nullptr);
736 
761  static bool incrementWithInitial(const QString &key,
762  uint64_t offset,
763  uint64_t initial,
764  time_t expiration,
765  uint64_t *value = nullptr,
766  MemcachedReturnType *returnType = nullptr);
767 
797  static bool incrementWithInitialByKey(const QString &groupKey,
798  const QString &key,
799  uint64_t offset,
800  uint64_t initial,
801  time_t expiration,
802  uint64_t *value = nullptr,
803  MemcachedReturnType *returnType = nullptr);
804 
820  static bool decrement(const QString &key,
821  uint32_t offset,
822  uint64_t *value = nullptr,
823  MemcachedReturnType *returnType = nullptr);
824 
843  static bool decrementByKey(const QString &groupKey,
844  const QString &key,
845  uint64_t offset,
846  uint64_t *value = nullptr,
847  MemcachedReturnType *returnType = nullptr);
848 
873  static bool decrementWithInitial(const QString &key,
874  uint64_t offset,
875  uint64_t initial,
876  time_t expiration,
877  uint64_t *value = nullptr,
878  MemcachedReturnType *returnType = nullptr);
879 
909  static bool decrementWithInitialByKey(const QString &groupKey,
910  const QString &key,
911  uint64_t offset,
912  uint64_t initial,
913  time_t expiration,
914  uint64_t *value = nullptr,
915  MemcachedReturnType *returnType = nullptr);
916 
929  static bool cas(const QString &key,
930  const QByteArray &value,
931  time_t expiration,
932  uint64_t cas,
933  MemcachedReturnType *returnType = nullptr);
934 
949  template <typename T>
950  static bool cas(const QString &key,
951  const T &value,
952  time_t expiration,
953  uint64_t cas,
954  MemcachedReturnType *returnType = nullptr);
955 
974  static bool casByKey(const QString &groupKey,
975  const QString &key,
976  const QByteArray &value,
977  time_t expiration,
978  uint64_t cas,
979  MemcachedReturnType *returnType = nullptr);
980 
1001  template <typename T>
1002  static bool casByKey(const QString &groupKey,
1003  const QString &key,
1004  const T &value,
1005  time_t expiration,
1006  uint64_t cas,
1007  MemcachedReturnType *returnType = nullptr);
1008 
1017  static bool flushBuffers(MemcachedReturnType *returnType = nullptr);
1018 
1030  static bool flush(time_t expiration, MemcachedReturnType *returnType = nullptr);
1031 
1045  static QHash<QString, QByteArray> mget(const QStringList &keys,
1046  QHash<QString, uint64_t> *casValues = nullptr,
1047  MemcachedReturnType *returnType = nullptr);
1048 
1064  template <typename T>
1065  static QHash<QString, T> mget(const QStringList &keys,
1066  QHash<QString, uint64_t> *casValues = nullptr,
1067  MemcachedReturnType *returnType = nullptr);
1068 
1088  static QHash<QString, QByteArray> mgetByKey(const QString &groupKey,
1089  const QStringList &keys,
1090  QHash<QString, uint64_t> *casValues = nullptr,
1091  MemcachedReturnType *returnType = nullptr);
1092 
1114  template <typename T>
1115  static QHash<QString, T> mgetByKey(const QString &groupKey,
1116  const QStringList &keys,
1117  QHash<QString, uint64_t> *casValues = nullptr,
1118  MemcachedReturnType *returnType = nullptr);
1119 
1129  static bool
1130  touch(const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
1131 
1146  static bool touchByKey(const QString &groupKey,
1147  const QString &key,
1148  time_t expiration,
1149  MemcachedReturnType *returnType = nullptr);
1150 
1154  static QString errorString(Context *c, MemcachedReturnType rt);
1155 
1159  static QVersionNumber libMemcachedVersion();
1160 
1161 protected:
1163 
1167  virtual bool setup(Application *app) override;
1168 };
1169 
1170 template <typename T>
1171 bool Memcached::set(const QString &key,
1172  const T &value,
1173  time_t expiration,
1174  MemcachedReturnType *returnType)
1175 {
1176  QByteArray data;
1177 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1178  QDataStream out(&data, QIODevice::WriteOnly);
1179 #else
1181 #endif
1182  out << value;
1183  return Memcached::set(key, data, expiration, returnType);
1184 }
1185 
1186 template <typename T>
1187 bool Memcached::setByKey(const QString &groupKey,
1188  const QString &key,
1189  const T &value,
1190  time_t expiration,
1191  MemcachedReturnType *returnType)
1192 {
1193  QByteArray data;
1194 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1195  QDataStream out(&data, QIODevice::WriteOnly);
1196 #else
1198 #endif
1199  out << value;
1200  return Memcached::setByKey(groupKey, key, data, expiration, returnType);
1201 }
1202 
1203 template <typename T>
1204 bool Memcached::add(const QString &key,
1205  const T &value,
1206  time_t expiration,
1207  MemcachedReturnType *returnType)
1208 {
1209  QByteArray data;
1210 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1211  QDataStream out(&data, QIODevice::WriteOnly);
1212 #else
1214 #endif
1215  out << value;
1216  return Memcached::add(key, data, expiration, returnType);
1217 }
1218 
1219 template <typename T>
1220 bool Memcached::addByKey(const QString &groupKey,
1221  const QString &key,
1222  const T &value,
1223  time_t expiration,
1224  MemcachedReturnType *returnType)
1225 {
1226  QByteArray data;
1227 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1228  QDataStream out(&data, QIODevice::WriteOnly);
1229 #else
1231 #endif
1232  out << value;
1233  return Memcached::addByKey(groupKey, key, data, expiration, returnType);
1234 }
1235 
1236 template <typename T>
1237 bool Memcached::replace(const QString &key,
1238  const T &value,
1239  time_t expiration,
1240  MemcachedReturnType *returnType)
1241 {
1242  QByteArray data;
1243 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1244  QDataStream out(&data, QIODevice::WriteOnly);
1245 #else
1247 #endif
1248  out << value;
1249  return Memcached::replace(key, data, expiration, returnType);
1250 }
1251 
1252 template <typename T>
1253 bool Memcached::replaceByKey(const QString &groupKey,
1254  const QString &key,
1255  const T &value,
1256  time_t expiration,
1257  MemcachedReturnType *returnType)
1258 {
1259  QByteArray data;
1260 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1261  QDataStream out(&data, QIODevice::WriteOnly);
1262 #else
1264 #endif
1265  out << value;
1266  return Memcached::replaceByKey(groupKey, key, data, expiration, returnType);
1267 }
1268 
1269 template <typename T>
1270 T Memcached::get(const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1271 {
1272  T retVal;
1273  QByteArray ba = Memcached::get(key, cas, returnType);
1274  if (!ba.isEmpty()) {
1275 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1277 #else
1279 #endif
1280  in >> retVal;
1281  }
1282  return retVal;
1283 }
1284 
1285 template <typename T>
1286 T Memcached::getByKey(const QString &groupKey,
1287  const QString &key,
1288  uint64_t *cas,
1289  MemcachedReturnType *returnType)
1290 {
1291  T retVal;
1292  QByteArray ba = Memcached::getByKey(groupKey, key, cas, returnType);
1293  if (!ba.isEmpty()) {
1294 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1296 #else
1298 #endif
1299  in >> retVal;
1300  }
1301  return retVal;
1302 }
1303 
1304 template <typename T>
1305 bool Memcached::cas(const QString &key,
1306  const T &value,
1307  time_t expiration,
1308  uint64_t cas,
1309  MemcachedReturnType *returnType)
1310 {
1311  QByteArray data;
1312 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1313  QDataStream out(&data, QIODevice::WriteOnly);
1314 #else
1316 #endif
1317  out << value;
1318  return Memcached::cas(key, data, expiration, cas, returnType);
1319 }
1320 
1321 template <typename T>
1322 bool Memcached::casByKey(const QString &groupKey,
1323  const QString &key,
1324  const T &value,
1325  time_t expiration,
1326  uint64_t cas,
1327  MemcachedReturnType *returnType)
1328 {
1329  QByteArray data;
1330 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1331  QDataStream out(&data, QIODevice::WriteOnly);
1332 #else
1334 #endif
1335  out << value;
1336  return Memcached::casByKey(groupKey, key, data, expiration, cas, returnType);
1337 }
1338 
1339 template <typename T>
1341  QHash<QString, uint64_t> *casValues,
1342  MemcachedReturnType *returnType)
1343 {
1344  QHash<QString, T> hash;
1345  QHash<QString, QByteArray> _data = Memcached::mget(keys, casValues, returnType);
1346  if (!_data.empty()) {
1347  auto i = _data.constBegin();
1348  while (i != _data.constEnd()) {
1349  T retVal;
1350  QDataStream in(i.value());
1351  in >> retVal;
1352  hash.insert(i.key(), retVal);
1353  ++i;
1354  }
1355  }
1356  return hash;
1357 }
1358 
1359 template <typename T>
1361  const QStringList &keys,
1362  QHash<QString, uint64_t> *casValues,
1363  MemcachedReturnType *returnType)
1364 {
1365  QHash<QString, T> hash;
1366  QHash<QString, QByteArray> _data = Memcached::mgetByKey(groupKey, keys, casValues, returnType);
1367  if (!_data.empty()) {
1368  auto i = _data.constBegin();
1369  while (i != _data.constEnd()) {
1370  T retVal;
1371  QDataStream in(i.value());
1372  in >> retVal;
1373  hash.insert(i.key(), retVal);
1374  ++i;
1375  }
1376  }
1377  return hash;
1378 }
1379 
1380 } // namespace Cutelyst
1381 
1382 #endif // CUTELYSTMEMCACHED_H
bool empty() const const
static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:422
static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:472
iterator insert(const Key &key, const T &value)
static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:517
static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1170
bool isEmpty() const const
static QHash< QString, QByteArray > mgetByKey(const QString &groupKey, const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1349
static const time_t expirationNotAdd
Definition: memcached.h:691
static QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:567
static QHash< QString, QByteArray > mget(const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1271
The Cutelyst Context.
Definition: context.h:38
const_iterator constEnd() const const
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:327
const_iterator constBegin() const const
static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1123
The Cutelyst Application.
Definition: application.h:42
static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:622
Cutelyst Memcached plugin.
Definition: memcached.h:157
static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:377
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:282