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.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") 27 , d_ptr(new MemcachedSessionStorePrivate)
31 "construct MemachedSessionStore",
32 "you have to specifiy a pointer to the Application object");
33 const QVariantMap map =
34 app->
engine()->
config(QStringLiteral(
"Cutelyst_MemcachedSessionStore_Plugin"));
35 d->groupKey = map.value(QStringLiteral(
"group_key")).toString();
49 const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
50 data = hash.
value(key, defaultValue);
60 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
61 data.insert(key, value);
62 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
63 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
71 QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
73 c->
setStash(SESSION_STORE_MEMCD_DATA, data);
74 c->
setStash(SESSION_STORE_MEMCD_SAVE,
true);
90 d->groupKey = groupKey;
96 const QVariant sessionVariant = c->
stash(SESSION_STORE_MEMCD_DATA);
97 if (!sessionVariant.
isNull()) {
98 data = sessionVariant.
toHash();
102 const static QString sessionPrefix =
104 const QString sessionKey = sessionPrefix + sid;
107 if (!c->stash(SESSION_STORE_MEMCD_SAVE).toBool()) {
111 const QVariantHash data = c->
stash(SESSION_STORE_MEMCD_DATA).toHash();
113 if (data.isEmpty()) {
121 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to remove session from Memcached.");
125 const time_t expires = data.value(QStringLiteral(
"expires")).value<time_t>();
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(SESSION_STORE_MEMCD_DATA, data);
148 #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 const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) final
void setStash(const QString &key, const QVariant &value)
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
bool isNull() const const
void stash(const QVariantHash &unite)
QVariantMap config(const QString &entity) const
user configuration for the application
bool isEmpty() const const
The Cutelyst namespace holds all public Cutelyst API.
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)
Application * app() const noexcept
void afterDispatch(Cutelyst::Context *c)
The Cutelyst Application.
Engine * engine() const noexcept
QString applicationName()
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)