What is The Memcached Tool
The Memcached Tool gives access to memcached server as if it were a python dictionary. Memcached provides a fast but transient storage, compared with ZODB not-so-fast but persistent and historized storage.
Requirements
A network-reachable memcached server or its variant
python-memcached module on all machines using Memcached Tool
Note: those requirements may be available as packages for your distribution. Also don't forget to take a look at Nexedi's repositories where we maintain some packages.
How to enable the use of Memcached Tool
Memcached Tool is automatically enabled if python-memcached library is installed.
How to store objects with Memcached Tool
First, you must keep in mind that objects stored in memcached can be lost at any time, because of memcache design. You should therefore never store any valuable data in memcached without storing it in a safe place. Objects will be deleted when memcached is stopped or if it runs out of memory (depending on configuration, see memcached manual). Or you can use 'persistent' variant of memcached like Flare.
Here is an example on how to store and retrieve an object using Memcached Tool :
1 memcached_tool = context.getPortalObject().portal_memcached
2 memcached_dict = memcached_tool.getMemcachedDict(key_prefix='my_first_test')
3 value = memcached_dict['some_key']
4 print value
5 if value is None:
6 value = 0
7 memcached_dict['some_key'] = value + 1
8 return printed
getMemcachedDict
This method takes one argument which allows to uniquely identify the use of memcached. It is a similar concept to Caching Method id, and allows multiple concurrent uses of a single memcached server.
Warnings
Because of python-memcached (and of memcached itself probably) design, you cannot
- iterate/list the dictionary content
get a KeyError when accessing a missing item (you'll get a None with no error instead)
Because of Memcached Tool design, you must explicitly store any modified objects. Unlike ZODB, it cannot (yet) detect changes in object's properties.
How to configure Memcached Tool
You can configure the ip:port used by Memcached Tool using its dedicated management screen, available in your instance at portal_memcached/memcached_tool_configure .