Skip to content. | Skip to navigation
plone.memoize and memcached
plone.memoize has built-in support for caching data in memcached, but it's not activated by default. In this article I want to show how to turn it on.
Intruduction
By default results of functions that use @plone.memoize.ram.cache decorator are stored in RAMCache - zope utility for storing data. But in ram.py file from plone.memoize you can find MemcacheAdapter class. This adapter gives possibility to store this data in memcached. To turn it on you must register ICacheCooser adapter.
Code
memcached.py:
# -*- coding: utf-8 -*-
from threading import local
from pylibmc import Client
from plone.memoize.ram import MemcacheAdapter
class MemcacheChooserUtility(object):
"""
See ICacheChooser.
"""
_v_thread_local = local()
def getClient(self):
"""
Return thread local connection to memcached.
"""
connection = getattr(self._v_thread_local, 'connection', None)
if connection is None:
connection = Client(['127.0.0.1:11211'])
self._v_thread_local.connection = connection
return connection
def __call__(self, fun_name):
"""
Create new adapter for plone.memoize.ram.
"""
return MemcacheAdapter(client=self.getClient(), globalkey=fun_name)
overrides.zcml:
<configure xmlns="http://namespaces.zope.org/zope">
<utility
provides="plone.memoize.interfaces.ICacheChooser"
factory=".memcached.MemcacheChooserUtility"
/>
</configure>
Installation
Add pylibmc to buildout. Use 0.7.4 - last version compatible with python 2.4. You can also use python-memcached, but I prefer pylibmc because this library reopen connection after memcached restart automatically.
Remember to include overrides.zcml in instance configuration.
Pros and cons
Advantages:
- one cache for all instances on ZEO
- restarting of Zope do not clear cache
Disadvantages:
- no invalidating (but there is workaround - in next article)


