6 #include "memcachedsessionstore_p.h"
8 #include <Cutelyst/Application>
9 #include <Cutelyst/Engine>
10 #include <Cutelyst/Context>
11 #include <Cutelyst/Plugins/Memcached/Memcached>
13 #include <QLoggingCategory>
14 #include <QCoreApplication>
18 Q_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")
23 static QVariantHash loadMemcSessionData(
Context *c,
const QString &sid,
const QString &groupKey);
26 SessionStore(parent), d_ptr(new MemcachedSessionStorePrivate)
29 Q_ASSERT_X(app,
"construct MemachedSessionStore",
"you have to specifiy a pointer to the Application object");
30 const QVariantMap map = app->
engine()->
config(QStringLiteral(
"Cutelyst_MemcachedSessionStore_Plugin"));
31 d->groupKey = map.value(QStringLiteral(
"group_key")).toString();
43 const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
44 data = hash.value(key, defaultValue);
51 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
52 data.insert(key, value);
53 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
54 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
62 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
64 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
65 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
81 d->groupKey = groupKey;
84 QVariantHash loadMemcSessionData(
Context *c,
const QString &sid,
const QString &groupKey)
87 const QVariant sessionVariant = c->
stash(SESSION_STORE_MEMCD_DATA);
88 if (!sessionVariant.isNull()) {
89 data = sessionVariant.toHash();
93 const static QString sessionPrefix = QCoreApplication::applicationName() + QLatin1String(
"_sess_");
94 const QString sessionKey = sessionPrefix + sid;
97 if (!c->stash(SESSION_STORE_MEMCD_SAVE).toBool()) {
101 const QVariantHash data = c->
stash(SESSION_STORE_MEMCD_DATA).toHash();
103 if (data.isEmpty()) {
105 if (groupKey.isEmpty()) {
106 ok = Memcached::remove(sessionKey);
108 ok = Memcached::removeByKey(groupKey, sessionKey);
111 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to remove session from Memcached.");
115 const time_t expires = data.value(QStringLiteral(
"expires")).value<time_t>();
116 if (groupKey.isEmpty()) {
122 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to store session to Memcached.");
127 if (groupKey.isEmpty()) {
128 data = Memcached::get<QVariantHash>(sessionKey);
130 data = Memcached::getByKey<QVariantHash>(groupKey, sessionKey);
133 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
138 #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
Memcached based session store.
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 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)
The Cutelyst namespace holds all public Cutelyst API.