6#include "memcachedsessionstore_p.h"
8#include <Cutelyst/Application>
9#include <Cutelyst/Context>
10#include <Cutelyst/Engine>
11#include <Cutelyst/Plugins/Memcached/Memcached>
13#include <QCoreApplication>
14#include <QLoggingCategory>
18Q_LOGGING_CATEGORY(C_MEMCACHEDSESSIONSTORE,
"cutelyst.plugin.memcachedsessionstore", QtWarningMsg)
20#define SESSION_STORE_MEMCD_SAVE QStringLiteral("_c_session_store_memcd_save")
21#define SESSION_STORE_MEMCD_DATA QStringLiteral("_c_session_store_memcd_data")
27 , d_ptr(new MemcachedSessionStorePrivate)
31 "construct MemachedSessionStore",
32 "you have to specifiy a pointer to the Application object");
33 const QVariantMap map =
34 app->
engine()->
config(QStringLiteral(
"Cutelyst_MemcachedSessionStore_Plugin"));
35 d->groupKey = map.value(QStringLiteral(
"group_key")).toString();
49 const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
50 data = hash.
value(key, defaultValue);
60 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
61 data.insert(key, value);
62 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
63 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
71 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
73 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
74 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
90 d->groupKey = groupKey;
96 const QVariant sessionVariant = c->
stash(SESSION_STORE_MEMCD_DATA);
97 if (!sessionVariant.
isNull()) {
98 data = sessionVariant.
toHash();
102 const static QString sessionPrefix =
104 const QString sessionKey = sessionPrefix + sid;
107 if (!c->stash(SESSION_STORE_MEMCD_SAVE).toBool()) {
111 const QVariantHash data = c->
stash(SESSION_STORE_MEMCD_DATA).toHash();
113 if (data.isEmpty()) {
115 if (groupKey.isEmpty()) {
116 ok = Memcached::remove(sessionKey);
118 ok = Memcached::removeByKey(groupKey, sessionKey);
121 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to remove session from Memcached.");
125 const time_t expires = data.value(QStringLiteral(
"expires")).value<time_t>();
132 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to store session to Memcached.");
143 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
148#include "moc_memcachedsessionstore.cpp"
The Cutelyst Application.
Engine * engine() const noexcept
void afterDispatch(Cutelyst::Context *c)
void stash(const QVariantHash &unite)
void setStash(const QString &key, const QVariant &value)
Application * app() const noexcept
QVariantMap config(const QString &entity) const
user configuration for the application
virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) final
MemcachedSessionStore(Application *app, QObject *parent=nullptr)
void setGroupKey(const QString &groupKey)
virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue) final
virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) final
virtual bool deleteExpiredSessions(Context *c, quint64 expires) final
static QByteArray get(const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
static QByteArray getByKey(const QString &groupKey, const QString &key, uint64_t *cas=nullptr, MemcachedReturnType *returnType=nullptr)
SessionStore(QObject *parent=nullptr)
The Cutelyst namespace holds all public Cutelyst API.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
bool isEmpty() const const
bool isNull() const const
QHash< QString, QVariant > toHash() const const