19 #include "memcachedsessionstore_p.h"
21 #include <Cutelyst/Application>
22 #include <Cutelyst/Engine>
23 #include <Cutelyst/Context>
24 #include <Cutelyst/Plugins/Memcached/Memcached>
26 #include <QLoggingCategory>
27 #include <QCoreApplication>
29 using namespace Cutelyst;
31 Q_LOGGING_CATEGORY(C_MEMCACHEDSESSIONSTORE,
"cutelyst.plugin.memcachedsessionstore", QtWarningMsg)
33 #define SESSION_STORE_MEMCD_SAVE QStringLiteral("_c_session_store_memcd_save")
34 #define SESSION_STORE_MEMCD_DATA QStringLiteral("_c_session_store_memcd_data")
39 SessionStore(parent), d_ptr(new MemcachedSessionStorePrivate)
42 Q_ASSERT_X(app,
"construct MemachedSessionStore",
"you have to specifiy a pointer to the Application object");
43 const QVariantMap map = app->
engine()->
config(QStringLiteral(
"Cutelyst_MemcachedSessionStore_Plugin"));
44 d->groupKey = map.value(QStringLiteral(
"group_key")).toString();
56 const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
57 data = hash.
value(key, defaultValue);
64 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
65 data.insert(key, value);
66 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
67 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
75 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
77 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
78 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
94 d->groupKey = groupKey;
100 const QVariant sessionVariant = c->
stash(SESSION_STORE_MEMCD_DATA);
101 if (!sessionVariant.
isNull()) {
102 data = sessionVariant.
toHash();
107 const QString sessionKey = sessionPrefix + sid;
110 if (!c->stash(SESSION_STORE_MEMCD_SAVE).toBool()) {
114 const QVariantHash data = c->
stash(SESSION_STORE_MEMCD_DATA).toHash();
116 if (data.isEmpty()) {
124 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to remove session from Memcached.");
128 const time_t expires = data.value(QStringLiteral(
"expires")).value<time_t>();
135 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to store session to Memcached.");
141 data = Memcached::get<QVariantHash>(sessionKey);
143 data = Memcached::getByKey<QVariantHash>(groupKey, sessionKey);
146 c->setStash(SESSION_STORE_MEMCD_DATA, data);
151 #include "moc_memcachedsessionstore.cpp"
virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) final
Memcached based session store.
QHash< QString, QVariant > toHash() const
virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) final
void setStash(const QString &key, const QVariant &value)
Application * app() const
MemcachedSessionStore(Application *app, QObject *parent=nullptr)
virtual bool deleteExpiredSessions(Context *c, quint64 expires) final
virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue) final
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
static bool removeByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType=nullptr)
static bool remove(const QString &key, MemcachedReturnType *returnType=nullptr)
void setGroupKey(const QString &groupKey)
void afterDispatch(Cutelyst::Context *c)
The Cutelyst Application.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVariantMap config(const QString &entity) const
user configuration for the application
QString applicationName()
void stash(const QVariantHash &unite)
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)