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> 19 Q_LOGGING_CATEGORY(C_MEMCACHEDSESSIONSTORE,
"cutelyst.plugin.sessionmemcached", QtWarningMsg)
21 const QString MemcachedSessionStorePrivate::stashKeyMemcdSave{u
"_c_session_store_memcd_save"_s};
22 const QString MemcachedSessionStorePrivate::stashKeyMemcdData{u
"_c_session_store_memcd_data"_s};
29 , d_ptr(new MemcachedSessionStorePrivate)
33 "construct MemachedSessionStore",
34 "you have to specifiy a pointer to the Application object");
35 const QVariantMap map = app->
engine()->
config(u
"Cutelyst_MemcachedSessionStore_Plugin"_s);
36 d->groupKey = map.value(u
"group_key"_s).toString().toLatin1();
39 MemcachedSessionStore::~MemcachedSessionStore() =
default;
48 const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
49 data = hash.
value(key, defaultValue);
53 bool MemcachedSessionStore::storeSessionData(
Context *c,
59 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
60 data.insert(key, value);
61 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdData, data);
62 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdSave,
true);
70 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
72 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdData, data);
73 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdSave,
true);
78 bool MemcachedSessionStore::deleteExpiredSessions(
Context *c, quint64 expires)
86 void MemcachedSessionStore::setGroupKey(
const QByteArray &groupKey)
89 d->groupKey = groupKey;
95 const QVariant sessionVariant = c->
stash(MemcachedSessionStorePrivate::stashKeyMemcdData);
96 if (!sessionVariant.
isNull()) {
97 data = sessionVariant.
toHash();
103 const QByteArray sessionKey = sessionPrefix + sid;
106 if (!c->stash(MemcachedSessionStorePrivate::stashKeyMemcdSave).toBool()) {
110 const QVariantHash data =
111 c->
stash(MemcachedSessionStorePrivate::stashKeyMemcdData).toHash();
113 if (data.isEmpty()) {
116 ok = Memcached::remove(sessionKey);
118 ok = Memcached::removeByKey(groupKey, sessionKey);
121 qCWarning(C_MEMCACHEDSESSIONSTORE)
122 <<
"Failed to remove session from Memcached." << groupKey << sessionKey;
126 const auto expires = data.value(u
"expires"_s).value<time_t>();
128 ok = Memcached::set(sessionKey, data, expires);
130 ok = Memcached::setByKey(groupKey, sessionKey, data, expires);
133 qCWarning(C_MEMCACHEDSESSIONSTORE) <<
"Failed to store session to Memcached.";
139 data = Memcached::get<QVariantHash>(sessionKey);
141 data = Memcached::getByKey<QVariantHash>(groupKey, sessionKey);
144 c->setStash(MemcachedSessionStorePrivate::stashKeyMemcdData, data);
149 #include "moc_memcachedsessionstore.cpp" Memcached based session store.
QHash< QString, QVariant > toHash() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
void setStash(const QString &key, const QVariant &value)
bool isNull() const const
void stash(const QVariantHash &unite)
QVariantMap config(const QString &entity) const
The Cutelyst namespace holds all public Cutelyst API.
Application * app() const noexcept
void afterDispatch(Cutelyst::Context *c)
Abstract class to create a session store.
QByteArray toLatin1() const const
The Cutelyst application.
Engine * engine() const noexcept
QString applicationName()