Cutelyst  2.13.0
memcachedsessionstore.cpp
1 /*
2  * Copyright (C) 2017 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "memcachedsessionstore_p.h"
20 
21 #include <Cutelyst/Application>
22 #include <Cutelyst/Engine>
23 #include <Cutelyst/Context>
24 #include <Cutelyst/Plugins/Memcached/Memcached>
25 
26 #include <QLoggingCategory>
27 #include <QCoreApplication>
28 
29 using namespace Cutelyst;
30 
31 Q_LOGGING_CATEGORY(C_MEMCACHEDSESSIONSTORE, "cutelyst.plugin.memcachedsessionstore", QtWarningMsg)
32 
33 #define SESSION_STORE_MEMCD_SAVE QStringLiteral("_c_session_store_memcd_save")
34 #define SESSION_STORE_MEMCD_DATA QStringLiteral("_c_session_store_memcd_data")
35 
36 static QVariantHash loadMemcSessionData(Context *c, const QString &sid, const QString &groupKey);
37 
39  SessionStore(parent), d_ptr(new MemcachedSessionStorePrivate)
40 {
42  Q_ASSERT_X(app, "construct MemachedSessionStore", "you have to specifiy a pointer to the Application object");
43  const QVariantMap map = app->engine()->config(QStringLiteral("Cutelyst_MemcachedSessionStore_Plugin"));
44  d->groupKey = map.value(QStringLiteral("group_key")).toString();
45 }
46 
48 {
49 
50 }
51 
52 QVariant MemcachedSessionStore::getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue)
53 {
54  QVariant data;
56  const QVariantHash hash = loadMemcSessionData(c, sid, d->groupKey);
57  data = hash.value(key, defaultValue);
58  return data;
59 }
60 
61 bool MemcachedSessionStore::storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value)
62 {
64  QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
65  data.insert(key, value);
66  c->setStash(SESSION_STORE_MEMCD_DATA, data);
67  c->setStash(SESSION_STORE_MEMCD_SAVE, true);
68 
69  return true;
70 }
71 
72 bool MemcachedSessionStore::deleteSessionData(Context *c, const QString &sid, const QString &key)
73 {
75  QVariantHash data = loadMemcSessionData(c, sid, d->groupKey);
76  data.remove(key);
77  c->setStash(SESSION_STORE_MEMCD_DATA, data);
78  c->setStash(SESSION_STORE_MEMCD_SAVE, true);
79 
80  return true;
81 }
82 
84 {
85  Q_UNUSED(c)
86  Q_UNUSED(expires)
87 
88  return true;
89 }
90 
91 void MemcachedSessionStore::setGroupKey(const QString &groupKey)
92 {
94  d->groupKey = groupKey;
95 }
96 
97 QVariantHash loadMemcSessionData(Context *c, const QString &sid, const QString &groupKey)
98 {
99  QVariantHash data;
100  const QVariant sessionVariant = c->stash(SESSION_STORE_MEMCD_DATA);
101  if (!sessionVariant.isNull()) {
102  data = sessionVariant.toHash();
103  return data;
104  }
105 
106  const static QString sessionPrefix = QCoreApplication::applicationName() + QLatin1String("_sess_");
107  const QString sessionKey = sessionPrefix + sid;
108 
109  QObject::connect(c->app(), &Application::afterDispatch, c, [=] () {
110  if (!c->stash(SESSION_STORE_MEMCD_SAVE).toBool()) {
111  return;
112  }
113 
114  const QVariantHash data = c->stash(SESSION_STORE_MEMCD_DATA).toHash();
115 
116  if (data.isEmpty()) {
117  bool ok = false;
118  if (groupKey.isEmpty()) {
119  ok = Memcached::remove(sessionKey);
120  } else {
121  ok = Memcached::removeByKey(groupKey, sessionKey);
122  }
123  if (!ok) {
124  qCWarning(C_MEMCACHEDSESSIONSTORE, "Failed to remove session from Memcached.");
125  }
126  } else {
127  bool ok = false;
128  const time_t expires = data.value(QStringLiteral("expires")).value<time_t>();
129  if (groupKey.isEmpty()) {
130  ok = Memcached::set(sessionKey, data, expires);
131  } else {
132  ok = Memcached::setByKey(groupKey, sessionKey, data, expires);
133  }
134  if (!ok) {
135  qCWarning(C_MEMCACHEDSESSIONSTORE, "Failed to store session to Memcached.");
136  }
137  }
138  });
139 
140  if (groupKey.isEmpty()) {
141  data = Memcached::get<QVariantHash>(sessionKey);
142  } else {
143  data = Memcached::getByKey<QVariantHash>(groupKey, sessionKey);
144  }
145 
146  c->setStash(SESSION_STORE_MEMCD_DATA, data);
147 
148  return data;
149 }
150 
151 #include "moc_memcachedsessionstore.cpp"
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::MemcachedSessionStore
Memcached based session store.
Definition: memcachedsessionstore.h:81
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::MemcachedSessionStore::storeSessionData
virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) final
Definition: memcachedsessionstore.cpp:61
Cutelyst::MemcachedSessionStore::~MemcachedSessionStore
~MemcachedSessionStore()
Definition: memcachedsessionstore.cpp:47
Cutelyst::Context::setStash
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:225
Cutelyst::MemcachedSessionStore::MemcachedSessionStore
MemcachedSessionStore(Application *app, QObject *parent=nullptr)
Definition: memcachedsessionstore.cpp:38
Cutelyst::Application::engine
Engine * engine() const
Definition: application.cpp:243
Cutelyst::SessionStore
Definition: session.h:29
Cutelyst::MemcachedSessionStore::deleteSessionData
virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) final
Definition: memcachedsessionstore.cpp:72
Cutelyst::MemcachedSessionStore::setGroupKey
void setGroupKey(const QString &groupKey)
Definition: memcachedsessionstore.cpp:91
Cutelyst::Application::afterDispatch
void afterDispatch(Cutelyst::Context *c)
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::MemcachedSessionStore::deleteExpiredSessions
virtual bool deleteExpiredSessions(Context *c, quint64 expires) final
Definition: memcachedsessionstore.cpp:83
Cutelyst::MemcachedSessionStore::getSessionData
virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue) final
Definition: memcachedsessionstore.cpp:52
Cutelyst::Engine::config
QVariantMap config(const QString &entity) const
user configuration for the application
Definition: engine.cpp:320
Cutelyst::Context::app
Application * app() const
Definition: context.cpp:104
Cutelyst::Memcached::setByKey
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:285
Cutelyst::Memcached::set
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:246
Cutelyst::Context::stash
void stash(const QVariantHash &unite)
Definition: context.h:558