Most Powerful Open Source ERP

How To Use Cache Tool

How To showing how to configure and cache method results throughout ERP5.
  • Last Update:2016-02-09
  • Version:001
  • Language:en

The ERP5Cache Tool provides a great and configurable way to cache results of methods. In ERP5, many things are dynamic and is different for each user, and change while the time is going on. But calculating all theses dynamics things takes some time, and often it is not necessary at all to compute values every time the user is asking for a new page. So it is very useful to cache some results, so that we can keep dynamic content and great speed.

Table of Contents

How to install

Now ERP5 is using the ERP5Cache Products. So you will need to install it. You can get it from subversion and an rpm will be soon available.

The Cache Tool will be automatically created with a new site. If you do not have it, then you should add an ERP5 Cache Tool by creating a new ERP5Type Tool and selecting ERP5 Cache Tool in the dialog that will be shown. Then you will need to update the erp5_core business template.

How to cache a method

Here is an example about how to define a cached method and to call it.

from Products.ERP5Type.Cache import CachingMethod
  
# Define the method
def computeAStrangeValue(value):
  # ... make calculation
  return new_value
# Cache the method
computeAStrangeValue = CachingMethod(computeAStrangeValue,
                                     'computeAStrangeValue')
# We call the cached method
new_value = computeAStrangeValue(value)

Notes:

  • You no longer must specify the expiry time of the cache when you define a new CachingMethod as the parameter will not be used.
  • You should not cache persistent objects, or you will get some "Should not load state when the connection is closed" errors. You can cache paths to documents instead.

How to configure multiple different caches

Currently, there is a nice web interface which allows to modify caches under http://host:port/your_site/portal_caches. Here you can add as many Cache Factories as you need. The default one, erp5_user_interface is used when you do not specify which cache factory you want to use.

For each cache factory, you can define the time when the cache will expire. This is very useful as you might have many CachingMethods defined everywhere and in this way you can change the value in a single place.

For each Cache Factory, you must add at least one cache plugin. A cache plugin is used for stating which kind of cache you want to use. There are currently 3 kinds of cache. The RAM Cache plugin is a local cache for each zope, it is erased each time you restart zope, and this cache is very fast. The Distributed RAM Cache plugin is slower but allows to share a cache value between many zope instances, this is useful only under a zeo environment.

You can notice that you can define 2 plugins (or more). You might think that it is a bit strange. But if it is well used, it can be very powerful. When there are many plugins and if you define a priority on each of them, then the cache tool will first try the first plugin, and if it can't find the value, it will try the second one. If the value is found, then it will update also the first plugin. So a very good use with zeo is to set a RAM Cache plugin as the first plugin, and then a Distributed RAM Cache for the second plugin.

How to configure different caches' storage backends

Currently there are 2 different cache plugins - RamCachePlugin and DistributedRamCachePlugin. They all have different backend storage where real cached values are stored. Except RamCachePlugin one needs to make some preparations in order to use the other two:

  • DistributedRamCache
    You need to install both a 'memcached' server and the python module for it, called 'python-memcache'. Detailed build and run instructions as well link to python module can be found at http://www.danga.com/memcached/.

How to know how many caches you should configure

I don't think we must configure too many different caches, as it not recommended. I think only a few is enough. May be this must be discussed seriously. A cache might be used for everything related to the user interface, another one to everything which is used by ERP5 core components. Also some business templates might define a new cache if required for a particular application.

How to specify which cache factory we want to use and the scope

An option is available when you define a Caching Method. It is cache_factory and it defines the id of the cache you want to use. Another option is used when you call the cached function, it is scope which is optional. We often use the user_id for the scope.

Here an example:

from Products.ERP5Type.Cache import CachingMethod

# Define the method
def computeAStrangeValue(value):
  # ... make calculation
  return new_value
# Cache the method
computeAStrangeValue = CachingMethod(computeAStrangeValue,
                                     'computeAStrangeValue',
                                     cache_factory='my_nice_cache')
# We call the cached method
user_id = context.portal_membership.getAuthenticatedMember().getUserName()
new_value = computeAStrangeValue(value,scope=user_id)

How to clear a particular cache

You can clear an entire cache, or clear only a cache for a particular scope.

# Delete a cache only for a particular scope
user_id = context.portal_membership.getAuthenticatedMember().getUserName()
context.portal_caches.clearCacheFactoryScope('erp5_user_interface',user_id)
# Delete a full cache
context.portal_caches.clearCacheFactory('erp5_user_interface')
# Delete all caches
context.portal_caches.clearCache()

Related Articles