Cutelyst  1.11.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 
127 class CUTELYST_PLUGIN_MEMCACHED_EXPORT Memcached : public Plugin
128 {
129  Q_OBJECT
130  Q_DECLARE_PRIVATE(Memcached)
131 public:
135  Memcached(Application *parent);
136 
140  virtual ~Memcached();
141 
166  End,
187  InProgress,
188  ServerTemporaryDisabled,
189  ServerMemoryAllocationFailure,
190  MaximumReturn,
191  PluginNotRegisterd
192  };
193  Q_ENUM(MemcachedReturnType)
194 
195 
199  void setDefaultConfig(const QVariantMap &defaultConfig);
200 
212  static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
213 
227  template< typename T>
228  static bool set(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
229 
245  static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
246 
263  template< typename T>
264  static bool setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
265 
280  static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
281 
298  template< typename T>
299  static bool add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
300 
319  static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
320 
341  template< typename T>
342  static bool addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
343 
358  static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
359 
376  template< typename T>
377  static bool replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
378 
397  static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
398 
419  template< typename T>
420  static bool replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType = nullptr);
421 
436  static QByteArray get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
437 
466  template< typename T>
467  static T get(const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
468 
487  static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
488 
521  template< typename T>
522  static T getByKey(const QString &groupKey, const QString &key, uint64_t *cas = nullptr, MemcachedReturnType *returnType = nullptr);
523 
531  static bool remove(const QString &key, MemcachedReturnType *returnType = nullptr);
532 
544  static bool removeByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType = nullptr);
545 
552  static bool exist(const QString &key, MemcachedReturnType *returnType = nullptr);
553 
564  static bool existByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType = nullptr);
565 
570  static const time_t expirationNotAdd;
571 
586  static bool increment(const QString &key, uint32_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
587 
605  static bool incrementByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
606 
629  static bool incrementWithInitial(const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
630 
658  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);
659 
674  static bool decrement(const QString &key, uint32_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
675 
693  static bool decrementByKey(const QString &groupKey, const QString &key, uint64_t offset, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
694 
717  static bool decrementWithInitial(const QString &key, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value = nullptr, MemcachedReturnType *returnType = nullptr);
718 
746  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);
747 
759  static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
760 
774  template< typename T>
775  static bool cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
776 
793  static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
794 
813  template< typename T>
814  static bool casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType = nullptr);
815 
823  static bool flushBuffers(MemcachedReturnType *returnType = nullptr);
824 
835  static bool flush(time_t expiration, MemcachedReturnType *returnType = nullptr);
836 
849  static QHash<QString, QByteArray> mget(const QStringList &keys, QHash<QString, uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
850 
865  template< typename T>
866  static QHash<QString, T> mget(const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
867 
885  static QHash<QString, QByteArray> mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
886 
906  template< typename T>
907  static QHash<QString, T> mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues = nullptr, MemcachedReturnType *returnType = nullptr);
908 
917  static bool touch(const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
918 
932  static bool touchByKey(const QString &groupKey, const QString &key, time_t expiration, MemcachedReturnType *returnType = nullptr);
933 
937  static QString errorString(Context *c, MemcachedReturnType rt);
938 
939 protected:
940  const QScopedPointer<MemcachedPrivate> d_ptr;
941 
945  virtual bool setup(Application *app) override;
946 
947 private:
948  Q_PRIVATE_SLOT(d_func(), void _q_postFork(Application*))
949 };
950 
951 template< typename T>
952 bool Memcached::set(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
953 {
954  QByteArray data;
955  QDataStream out(&data, QIODevice::WriteOnly);
956  out << value;
957  return Memcached::set(key, data, expiration, returnType);
958 }
959 
960 template< typename T>
961 bool Memcached::setByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
962 {
963  QByteArray data;
964  QDataStream out(&data, QIODevice::WriteOnly);
965  out << value;
966  return Memcached::setByKey(groupKey, key, data, expiration, returnType);
967 }
968 
969 template< typename T>
970 bool Memcached::add(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
971 {
972  QByteArray data;
973  QDataStream out(&data, QIODevice::WriteOnly);
974  out << value;
975  return Memcached::add(key, data, expiration, returnType);
976 }
977 
978 template< typename T>
979 bool Memcached::addByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
980 {
981  QByteArray data;
982  QDataStream out(&data, QIODevice::WriteOnly);
983  out << value;
984  return Memcached::addByKey(groupKey, key, data, expiration, returnType);
985 }
986 
987 template< typename T>
988 bool Memcached::replace(const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
989 {
990  QByteArray data;
991  QDataStream out(&data, QIODevice::WriteOnly);
992  out << value;
993  return Memcached::replace(key, data, expiration, returnType);
994 }
995 
996 template< typename T>
997 bool Memcached::replaceByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, MemcachedReturnType *returnType)
998 {
999  QByteArray data;
1000  QDataStream out(&data, QIODevice::WriteOnly);
1001  out << value;
1002  return Memcached::replaceByKey(groupKey, key, data, expiration, returnType);
1003 }
1004 
1005 template< typename T>
1006 T Memcached::get(const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1007 {
1008  T retVal;
1009  QByteArray ba = Memcached::get(key, cas, returnType);
1010  if (!ba.isEmpty()) {
1011  QDataStream in(&ba, QIODevice::ReadOnly);
1012  in >> retVal;
1013  }
1014  return retVal;
1015 }
1016 
1017 template< typename T>
1018 T Memcached::getByKey(const QString &groupKey, const QString &key, uint64_t *cas, MemcachedReturnType *returnType)
1019 {
1020  T retVal;
1021  QByteArray ba = Memcached::getByKey(groupKey, key, cas, returnType);
1022  if (!ba.isEmpty()) {
1023  QDataStream in(&ba, QIODevice::ReadOnly);
1024  in >> retVal;
1025  }
1026  return retVal;
1027 }
1028 
1029 template< typename T>
1030 bool Memcached::cas(const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1031 {
1032  QByteArray data;
1033  QDataStream out(&data, QIODevice::WriteOnly);
1034  out << value;
1035  return Memcached::cas(key, data, expiration, cas, returnType);
1036 }
1037 
1038 template< typename T>
1039 bool Memcached::casByKey(const QString &groupKey, const QString &key, const T &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType)
1040 {
1041  QByteArray data;
1042  QDataStream out(&data, QIODevice::WriteOnly);
1043  out << value;
1044  return Memcached::casByKey(groupKey, key, data, expiration, cas, returnType);
1045 }
1046 
1047 template< typename T>
1048 QHash<QString, T> Memcached::mget(const QStringList &keys, QHash<QString, uint64_t> *casValues, MemcachedReturnType *returnType)
1049 {
1050  QHash<QString, T> hash;
1051  QHash<QString,QByteArray> _data = Memcached::mget(keys, casValues, returnType);
1052  if (!_data.empty()) {
1053  auto i = _data.constBegin();
1054  while (i != _data.constEnd()) {
1055  T retVal;
1056  QDataStream in(i.value());
1057  in >> retVal;
1058  hash.insert(i.key(), retVal);
1059  ++i;
1060  }
1061  }
1062  return hash;
1063 }
1064 
1065 template< typename T>
1066 QHash<QString, T> Memcached::mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues, MemcachedReturnType *returnType)
1067 {
1068  QHash<QString, T> hash;
1069  QHash<QString,QByteArray> _data = Memcached::mgetByKey(groupKey, keys, casValues, returnType);
1070  if (!_data.empty()) {
1071  auto i = _data.constBegin();
1072  while (i != _data.constEnd()) {
1073  T retVal;
1074  QDataStream in(i.value());
1075  in >> retVal;
1076  hash.insert(i.key(), retVal);
1077  ++i;
1078  }
1079  }
1080  return hash;
1081 }
1082 
1083 }
1084 
1085 #endif // CUTELYSTMEMCACHED_H
static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:324
static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:366
static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:405
static bool casByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:960
static QHash< QString, QByteArray > mgetByKey(const QString &groupKey, const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1125
static QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:447
static QHash< QString, QByteArray > mget(const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1049
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:243
static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:920
The Cutelyst Application.
Definition: application.h:54
static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:500
Cutelyst Memcached plugin.
Definition: memcached.h:127
static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:285
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:204