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> 18 Q_LOGGING_CATEGORY(C_MEMCACHEDSESSIONSTORE,
"cutelyst.plugin.sessionmemcached", QtWarningMsg)
20 const QString MemcachedSessionStorePrivate::stashKeyMemcdSave{u
"_c_session_store_memcd_save"_qs};
21 const QString MemcachedSessionStorePrivate::stashKeyMemcdData{u
"_c_session_store_memcd_data"_qs};
28 , d_ptr(new MemcachedSessionStorePrivate)
32 "construct MemachedSessionStore",
33 "you have to specifiy a pointer to the Application object");
34 const QVariantMap map = app->
engine()->
config(u
"Cutelyst_MemcachedSessionStore_Plugin"_qs);
35 d->groupKey = map.value(u
"group_key"_qs).toString().toLatin1();
38 MemcachedSessionStore::~MemcachedSessionStore() =
default;
47 const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
48 data = hash.
value(key, defaultValue);
52 bool MemcachedSessionStore::storeSessionData(
Context *c,
58 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
59 data.insert(key, value);
60 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdData, data);
61 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdSave,
true);
69 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
71 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdData, data);
72 c->
setStash(MemcachedSessionStorePrivate::stashKeyMemcdSave,
true);
77 bool MemcachedSessionStore::deleteExpiredSessions(
Context *c, quint64 expires)
85 void MemcachedSessionStore::setGroupKey(
const QByteArray &groupKey)
88 d->groupKey = groupKey;
94 const QVariant sessionVariant = c->
stash(MemcachedSessionStorePrivate::stashKeyMemcdData);
95 if (!sessionVariant.
isNull()) {
96 data = sessionVariant.
toHash();
102 const QByteArray sessionKey = sessionPrefix + sid;
105 if (!c->stash(MemcachedSessionStorePrivate::stashKeyMemcdSave).toBool()) {
109 const QVariantHash data =
110 c->
stash(MemcachedSessionStorePrivate::stashKeyMemcdData).toHash();
112 if (data.isEmpty()) {
115 ok = Memcached::remove(sessionKey);
117 ok = Memcached::removeByKey(groupKey, sessionKey);
120 qCWarning(C_MEMCACHEDSESSIONSTORE)
121 <<
"Failed to remove session from Memcached." << groupKey << sessionKey;
125 const auto expires = data.value(u
"expires"_qs).value<time_t>();
127 ok = Memcached::set(sessionKey, data, expires);
129 ok = Memcached::setByKey(groupKey, sessionKey, data, expires);
132 qCWarning(C_MEMCACHEDSESSIONSTORE) <<
"Failed to store session to Memcached.";
138 data = Memcached::get<QVariantHash>(sessionKey);
140 data = Memcached::getByKey<QVariantHash>(groupKey, sessionKey);
143 c->setStash(MemcachedSessionStorePrivate::stashKeyMemcdData, data);
148 #include "moc_memcachedsessionstore.cpp" Memcached based session store.
QHash< QString, QVariant > toHash() const const
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
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString applicationName()