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>
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")
36 static QVariantHash loadMemcSessionData(
Context *c,
const QString &sid,
const QString &groupKey);
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;
97 QVariantHash loadMemcSessionData(
Context *c,
const QString &sid,
const QString &groupKey)
100 const QVariant sessionVariant = c->
stash(SESSION_STORE_MEMCD_DATA);
101 if (!sessionVariant.isNull()) {
102 data = sessionVariant.toHash();
106 const static QString sessionPrefix = QCoreApplication::applicationName() + QLatin1String(
"_sess_");
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()) {
118 if (groupKey.isEmpty()) {
119 ok = Memcached::remove(sessionKey);
121 ok = Memcached::removeByKey(groupKey, sessionKey);
124 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to remove session from Memcached.");
128 const time_t expires = data.value(QStringLiteral(
"expires")).value<time_t>();
129 if (groupKey.isEmpty()) {
135 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to store session to Memcached.");
140 if (groupKey.isEmpty()) {
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"