Cutelyst  1.11.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")
32 
33 #define SESSION_STORE_MEMCD_SAVE "__session_store_memcd_save"
34 #define SESSION_STORE_MEMCD_DATA "__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->setProperty(SESSION_STORE_MEMCD_DATA, data);
67  c->setProperty(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->setProperty(SESSION_STORE_MEMCD_DATA, data);
78  c->setProperty(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->property(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, &Context::destroyed, [=] () {
110  if (!c->property(SESSION_STORE_MEMCD_SAVE).toBool()) {
111  return;
112  }
113 
114  QVariantHash data = c->property(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->setProperty(SESSION_STORE_MEMCD_DATA, data);
147 
148  return data;
149 }
150 
151 #include "moc_memcachedsessionstore.cpp"
virtual bool storeSessionData(Context *c, const QString &sid, const QString &key, const QVariant &value) final
Memcached based session store.
Engine * engine() const
virtual bool deleteSessionData(Context *c, const QString &sid, const QString &key) final
MemcachedSessionStore(Application *app, QObject *parent=nullptr)
The Cutelyst Context.
Definition: context.h:50
virtual bool deleteExpiredSessions(Context *c, quint64 expires) final
virtual QVariant getSessionData(Context *c, const QString &sid, const QString &key, const QVariant &defaultValue) final
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
static bool setByKey(const QString &groupKey, const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:243
static bool removeByKey(const QString &groupKey, const QString &key, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:584
static bool remove(const QString &key, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:556
void setGroupKey(const QString &groupKey)
The Cutelyst Application.
Definition: application.h:54
QVariantMap config(const QString &entity) const
user configuration for the application
Definition: engine.cpp:383
static bool set(const QString &key, const QByteArray &value, time_t expiration, MemcachedReturnType *returnType=nullptr)
Definition: memcached.cpp:204