Cutelyst  2.13.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:
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>
1065 QHash<QString, T> Memcached::mget(const QStringList &keys, QHash<QString, uint64_t> *casValues, MemcachedReturnType *returnType)
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>
1083 QHash<QString, T> Memcached::mgetByKey(const QString &groupKey, const QStringList &keys, QHash<QString,uint64_t> *casValues, MemcachedReturnType *returnType)
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
Cutelyst::Plugin
Definition: plugin.h:30
Cutelyst::Memcached
Cutelyst Memcached plugin.
Definition: memcached.h:142
Cutelyst::Memcached::NotStored
@ NotStored
Definition: memcached.h:174
Cutelyst::Memcached::SomeErrors
@ SomeErrors
Definition: memcached.h:179
Cutelyst::Memcached::mget
static QHash< QString, QByteArray > mget(const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1093
Cutelyst::Memcached::ConnectionFailure
@ ConnectionFailure
Definition: memcached.h:164
Cutelyst::Memcached::ServerMarkedDead
@ ServerMarkedDead
Definition: memcached.h:191
Cutelyst::Memcached::End
@ End
Definition: memcached.h:181
Cutelyst::Memcached::replaceByKey
static bool replaceByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:447
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Memcached::UnknownReadFailure
@ UnknownReadFailure
Definition: memcached.h:167
Cutelyst::Memcached::Failure
@ Failure
Definition: memcached.h:162
Cutelyst::Memcached::HostLookupFailure
@ HostLookupFailure
Definition: memcached.h:163
Cutelyst::Memcached::NotFound
@ NotFound
Definition: memcached.h:176
Cutelyst::Memcached::casByKey
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
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Memcached::ReadFailure
@ ReadFailure
Definition: memcached.h:166
Cutelyst::Memcached::ProtocolError
@ ProtocolError
Definition: memcached.h:168
Cutelyst::Memcached::cas
static bool cas(const QString &key, const QByteArray &value, time_t expiration, uint64_t cas, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:964
Cutelyst::Memcached::WriteFailure
@ WriteFailure
Definition: memcached.h:165
Cutelyst::Memcached::addByKey
static bool addByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:366
Cutelyst::Memcached::Error
@ Error
Definition: memcached.h:171
Cutelyst::Memcached::KeyTooBig
@ KeyTooBig
Definition: memcached.h:195
Cutelyst::Memcached::NoServers
@ NoServers
Definition: memcached.h:180
Cutelyst::Memcached::Buffered
@ Buffered
Definition: memcached.h:188
Cutelyst::Memcached::Success
@ Success
Definition: memcached.h:161
Cutelyst::Memcached::Timeout
@ Timeout
Definition: memcached.h:187
Cutelyst::Memcached::Errno
@ Errno
Definition: memcached.h:184
Cutelyst::Memcached::expirationNotAdd
static const time_t expirationNotAdd
Definition: memcached.h:585
Cutelyst::Memcached::AuthContinue
@ AuthContinue
Definition: memcached.h:198
Cutelyst::Memcached::Stat
@ Stat
Definition: memcached.h:183
Cutelyst::Memcached::UnknownStatKey
@ UnknownStatKey
Definition: memcached.h:192
Cutelyst::Memcached::ServerError
@ ServerError
Definition: memcached.h:170
Cutelyst::Memcached::NotSupported
@ NotSupported
Definition: memcached.h:185
Cutelyst::Memcached::AuthFailure
@ AuthFailure
Definition: memcached.h:197
Cutelyst::Memcached::InvalidArguments
@ InvalidArguments
Definition: memcached.h:194
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Memcached::PartialRead
@ PartialRead
Definition: memcached.h:178
Cutelyst::Memcached::replace
static bool replace(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:408
Cutelyst::Memcached::DataDoesNotExist
@ DataDoesNotExist
Definition: memcached.h:173
Cutelyst::Memcached::getByKey
static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:542
Cutelyst::Memcached::InvalidHostProtocol
@ InvalidHostProtocol
Definition: memcached.h:190
Cutelyst::Memcached::AuthProblem
@ AuthProblem
Definition: memcached.h:196
Cutelyst::Memcached::Deleted
@ Deleted
Definition: memcached.h:182
Cutelyst::Memcached::ClientError
@ ClientError
Definition: memcached.h:169
Cutelyst::Memcached::get
static QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:489
Cutelyst::Memcached::FetchNotFinished
@ FetchNotFinished
Definition: memcached.h:186
Cutelyst::Memcached::E2Big
@ E2Big
Definition: memcached.h:193
Cutelyst::Memcached::add
static bool add(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:327
Cutelyst::Memcached::Deprecated
@ Deprecated
Definition: memcached.h:201
Cutelyst::Memcached::DataExists
@ DataExists
Definition: memcached.h:172
Cutelyst::Memcached::setByKey
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:285
Cutelyst::Memcached::ParseError
@ ParseError
Definition: memcached.h:199
Cutelyst::Memcached::MemoryAllocationFailure
@ MemoryAllocationFailure
Definition: memcached.h:177
Cutelyst::Memcached::Stored
@ Stored
Definition: memcached.h:175
Cutelyst::Memcached::BadKeyProvided
@ BadKeyProvided
Definition: memcached.h:189
Cutelyst::Memcached::set
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:246
Cutelyst::Memcached::ParseUserError
@ ParseUserError
Definition: memcached.h:200
Cutelyst::Memcached::MemcachedReturnType
MemcachedReturnType
Definition: memcached.h:160
Cutelyst::Memcached::mgetByKey
static QHash< QString, QByteArray > mgetByKey(const QString &groupKey, const QStringList &keys, QHash< QString, uint64_t > *casValues=nullptr, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:1168