Most Powerful Open Source ERP

How To Use Memcached Tool

How To showing how access a memcached server as if it were a python dictionary.
  • Last Update:2016-02-09
  • Version:001
  • Language:en

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.

Table of Contents

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:

memcached_tool = context.getPortalObject().portal_memcached
memcached_dict = memcached_tool.getMemcachedDict(key_prefix='my_first_test',
  plugin_path='portal_memcached/default_memcached_plugin')
value = memcached_dict['some_key']
print value
if value is None:
  value = 0
memcached_dict['some_key'] = value + 1
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 .

Related Articles