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") 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;
87 const QVariant sessionVariant = c->
stash(SESSION_STORE_MEMCD_DATA);
88 if (!sessionVariant.
isNull()) {
89 data = sessionVariant.
toHash();
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()) {
111 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to remove session from Memcached.");
115 const time_t expires = data.value(QStringLiteral(
"expires")).value<time_t>();
122 qCWarning(C_MEMCACHEDSESSIONSTORE,
"Failed to store session to Memcached.");
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" 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
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
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString applicationName()
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)