Cutelyst  2.14.2
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:
960  const QScopedPointer<MemcachedPrivate> d_ptr;
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  QDataStream out(&data, QIODevice::WriteOnly);
973  out << value;
974  return Memcached::set(key, data, expiration, returnType);
975 }
976 
977 template< typename T>
978 bool Memcached::setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
979 {
980  QByteArray data;
981  QDataStream out(&data, QIODevice::WriteOnly);
982  out << value;
983  return Memcached::setByKey(groupKey, key, data, expiration, returnType);
984 }
985 
986 template< typename T>
987 bool Memcached::add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
988 {
989  QByteArray data;
990  QDataStream out(&data, QIODevice::WriteOnly);
991  out << value;
992  return Memcached::add(key, data, expiration, returnType);
993 }
994 
995 template< typename T>
996 bool Memcached::addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
997 {
998  QByteArray data;
999  QDataStream out(&data, QIODevice::WriteOnly);
1000  out << value;
1001  return Memcached::addByKey(groupKey, key, data, expiration, returnType);
1002 }
1003 
1004 template< typename T>
1005 bool Memcached::replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1006 {
1007  QByteArray data;
1008  QDataStream out(&data, QIODevice::WriteOnly);
1009  out << value;
1010  return Memcached::replace(key, data, expiration, returnType);
1011 }
1012 
1013 template< typename T>
1014 bool Memcached::replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
1015 {
1016  QByteArray data;
1017  QDataStream out(&data, QIODevice::WriteOnly);
1018  out << value;
1019  return Memcached::replaceByKey(groupKey, key, data, expiration, returnType);
1020 }
1021 
1022 template< typename T>
1023 T Memcached::get(const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1024 {
1025  T retVal;
1026  QByteArray ba = Memcached::get(key, cas, returnType);
1027  if (!ba.isEmpty()) {
1028  QDataStream in(&ba, QIODevice::ReadOnly);
1029  in >> retVal;
1030  }
1031  return retVal;
1032 }
1033 
1034 template< typename T>
1035 T Memcached::getByKey(const QString &groupKey, const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1036 {
1037  T retVal;
1038  QByteArray ba = Memcached::getByKey(groupKey, key, cas, returnType);
1039  if (!ba.isEmpty()) {
1040  QDataStream in(&ba, QIODevice::ReadOnly);
1041  in >> retVal;
1042  }
1043  return retVal;
1044 }
1045 
1046 template< typename T>
1047 bool Memcached::cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1048 {
1049  QByteArray data;
1050  QDataStream out(&data, QIODevice::WriteOnly);
1051  out << value;
1052  return Memcached::cas(key, data, expiration, cas, returnType);
1053 }
1054 
1055 template< typename T>
1056 bool Memcached::casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1057 {
1058  QByteArray data;
1059  QDataStream out(&data, QIODevice::WriteOnly);
1060  out << value;
1061  return Memcached::casByKey(groupKey, key, data, expiration, cas, returnType);
1062 }
1063 
1064 template< typename T>
1066 {
1067  QHash<QString, T> hash;
1068  QHash<QString,QByteArray> _data = Memcached::mget(keys, casValues, returnType);
1069  if (!_data.empty()) {
1070  auto i = _data.constBegin();
1071  while (i != _data.constEnd()) {
1072  T retVal;
1073  QDataStream in(i.value());
1074  in >> retVal;
1075  hash.insert(i.key(), retVal);
1076  ++i;
1077  }
1078  }
1079  return hash;
1080 }
1081 
1082 template< typename T>
1084 {
1085  QHash<QString, T> hash;
1086  QHash<QString,QByteArray> _data = Memcached::mgetByKey(groupKey, keys, casValues, returnType);
1087  if (!_data.empty()) {
1088  auto i = _data.constBegin();
1089  while (i != _data.constEnd()) {
1090  T retVal;
1091  QDataStream in(i.value());
1092  in >> retVal;
1093  hash.insert(i.key(), retVal);
1094  ++i;
1095  }
1096  }
1097  return hash;
1098 }
1099 
1100 }
1101 
1102 #endif // CUTELYSTMEMCACHED_H
bool empty() const
static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:366
static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:408
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:447
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
bool isEmpty() const
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 QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:489
static QHash< QString, QByteArray > mget(const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1093
The Cutelyst Context.
Definition: context.h:51
const_iterator constEnd() const
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:285
const_iterator constBegin() const
static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:964
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:542
Cutelyst Memcached plugin.
Definition: memcached.h:142
static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:327
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:246