cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::Memcached Class Reference

Cutelyst Memcached plugin. More...

#include <Cutelyst/Plugins/Memcached/Memcached>

Inheritance diagram for Cutelyst::Memcached:

Protected Member Functions

bool setup (Application *app) override
 
- Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
int receivers (const char *signal) const const
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 

Additional Inherited Members

- Public Member Functions inherited from Cutelyst::Plugin
 Plugin (Application *parent)
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
QBindable< QStringbindableObjectName ()
 
bool blockSignals (bool block)
 
const QObjectListchildren () const const
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectInfo () const const
 
void dumpObjectTree () const const
 
QList< QByteArraydynamicPropertyNames () const const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (Qt::FindChildOptions options) const const
 
bool inherits (const char *className) const const
 
void installEventFilter (QObject *filterObj)
 
bool isQuickItemType () const const
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
QVariant property (const char *name) const const
 
 Q_CLASSINFO (Name, Value)
 
 Q_EMIT Q_EMIT
 
 Q_ENUM (...)
 
 Q_ENUM_NS (...)
 
 Q_ENUMS (...)
 
 Q_FLAG (...)
 
 Q_FLAG_NS (...)
 
 Q_FLAGS (...)
 
 Q_GADGET Q_GADGET
 
 Q_GADGET_EXPORT (EXPORT_MACRO)
 
 Q_INTERFACES (...)
 
 Q_INVOKABLE Q_INVOKABLE
 
 Q_MOC_INCLUDE Q_MOC_INCLUDE
 
 Q_NAMESPACE Q_NAMESPACE
 
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 
 Q_OBJECT Q_OBJECT
 
 Q_PROPERTY (...)
 
 Q_REVISION Q_REVISION
 
 Q_SET_OBJECT_NAME (Object)
 
 Q_SIGNAL Q_SIGNAL
 
 Q_SIGNALS Q_SIGNALS
 
 Q_SLOT Q_SLOT
 
 Q_SLOTS Q_SLOTS
 
qobject_cast (const QObject *object)
 
qobject_cast (QObject *object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setObjectName (QAnyStringView name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool setProperty (const char *name, QVariant &&value)
 
bool signalsBlocked () const const
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds interval, Qt::TimerType timerType)
 
QThreadthread () const const
 
- Static Public Member Functions inherited from QObject
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
- Public Attributes inherited from QObject
typedef QObjectList
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The Memcached plugin for Cutelyst can be used to store, retrieve, delete and modify data on a memcached general-purpose distributed memory caching system. It uses libmemcached to connect to a pool of memcached servers and to perform the caching operations. In order to build this plugin, the libmemcached development and header files have to be present at build time.

Basically all values are stored as QByteArray. So, to store simple types, simply convert them into a QByteArray and vice versa on retrieval. For more complex or custom types you can use QDataStream to serialize them into a QByteArray. For most methods in this plugin there are template functions for convenience that perform this serialization. The requirement to use them is that the types to store and get provide stream operators for QDataStream.

Configuration

The Memcached plugin can be configured in the application configuration file in the Cutelyst_Memcached_Plugin section. It uses the same configuration strings as libmemcached but in lowercase and without the dashes in front and for consistence '-' replaced by '_'. So –BINARY-PROTOCOL will be binary_protocol. To add servers and/or sockets use the servers configuration key. Servers can be added with name, port and weight, separated by ',' - multiple servers are separated by a ';'. To add sockets, use a full path as name. If no configuration has been set or if the servers configuration key is empty, a default server at localhost on port 11211 will be used.

Additional to the configuration options of libmemcached there are some plugin specific options:

compression

Type: bool
Default: fase

Enables compression of input values based on qCompress / zlib.

compression_level

Type: integer
Default: -1

The compression level used by qCompress(). Valid values are between 0 and 9, with 9 corresponding to the greatest compression. The value 0 corresponds to no compression at all. The default value is -1, which specifies zlib’s default compression.

compression_threshold

Type: integer
Default: 100

The compression size threshold in bytes. Only input values bigger than the threshold will be compressed.

encryption_key

Type: string
Default: empty

If set and not empty, AES encryption will be enabled for storing data on the memcached servers.

sasl_user

Type: string
Default: empty

If set and not empty, SASL authentication will be used to authenticate with the memcached server(s). Note that SASL support has to be enabled when building libmemcached.

sasl_password

Type: string
Default: empty

Password used for the SASL authentication with the memcached server(s).

Note
If you want to use non-ASCII key names, you have to enable the binary protocol.

To set default values directly in your application, use setDefaultConfig() or the overloaded constructor taking default values. Configuration values that can not be found in the application configuration file will be looked up for default values in that QVariantMap.

Configuration example

[Cutelyst_Memcached_Plugin]
servers=cache.example.com,11211,2;/path/to/memcached.sock,1
binary_protocol=true
namespace=foobar

Expiration times

Expiration times are set in seconds. If the value is bigger than 30 days, it is interpreted as a unix timestamp.

Logging and return types

Messages from this plugin are logged to the logging category cutelyst.plugin.memcached. All methods provide the possibility to specify a pointer to a Memcached::ReturnType variable that can provide further information about occurred errors if methods return false or empty results.

See also
Logging with Cutelyst

Usage example

#include <Cutelyst/Plugins/Memcached/Memcached>
bool MyCutelystApp::init()
{
// other initialization stuff
// ...
new Memcached(this);
// maybe more initialization stuff
//...
}
void MyController::index(Context *c)
{
QVariantList myData = Memcached::get<QVariantList>(QStringLiteral("myKey"));
if (myData.empty()) {
QSqlQuery q = CPreparedSqlQuery(QStringLiteral("SELECT * FROM myTable"));
if (q.exec()) {
myData = Sql::queryToHashList(q);
if (!myData.empty()) {
Memcached::set(QStringLiteral("myKey"), myData, 900);
}
}
}
// present your data
// ...
}
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToHashList(QSqlQuery &query)
Definition: sql.cpp:33
#define CPreparedSqlQuery(str)
Definition: sql.h:234
bool exec()

Build requirements

To build this plugin you need the development and header files for libmemcached and run cmake with -DPLUGIN_MEMCACHED:BOOL=ON.

Unit test

Enabling the build of the Memcached plugin will also enable the unit tests for this plugin. By default, the unit test will start its own memcached instance. Alternatively you can set the CUTELYST_MEMCACHED_TEST_SERVERS environment variable in your build environment to define different servers that you have to start by yourself. The syntax is the same as for adding servers in the configuration file. If you have for examble two servers, one on default location and another one on a unix socket, export the following environment variable befor running the tests:

export CUTELYST_MEMCACHED_TEST_SERVERS=localhost;/tmp/memcached.sock
Since
Cutelyst 1.11.0

Definition at line 166 of file memcached.h.

Member Function Documentation

◆ setup()