Back to index

plone3  3.1.7
Classes | Functions | Variables
plone.memoize.ram Namespace Reference

Classes

class  AbstractDict
class  MemcacheAdapter
class  RAMCacheAdapter

Functions

def choose_cache
def store_in_cache
def cache

Variables

tuple global_cache = ram.RAMCache()
 DontCache = volatile.DontCache
tuple MARKER = object()

Detailed Description

A cache decorator that uses RAMCache by default.

See README.txt and the `volatile` module for more details.

  >>> def cache_key(fun, first, second):
  ...     return (first, second)
  >>> @cache(cache_key)
  ... def pow(first, second):
  ...     print 'Someone or something called me'
  ...     return first ** second

  >>> pow(3, 2)
  Someone or something called me
  9
  >>> pow(3, 2)
  9

Let's cache another function:

  >>> @cache(cache_key)
  ... def add(first, second):
  ...     print 'Someone or something called me'
  ...     return first + second

  >>> add(3, 2)
  Someone or something called me
  5
  >>> add(3, 2)
  5

Now invalidate the cache for the `pow` function:

  >>> pow(3, 2)
  9
  >>> global_cache.invalidate('plone.memoize.ram.pow')
  >>> pow(3, 2)
  Someone or something called me
  9

Make sure that we only invalidated the cache for the `pow` function:

  >>> add(3, 2)
  5

  >>> global_cache.invalidateAll()

You can register an ICacheChooser utility to override the cache used
based on the function that is cached.  To do this, we'll first
unregister the already registered global `choose_cache` function:

  >>> sm = component.getGlobalSiteManager()
  >>> sm.unregisterUtility(choose_cache)
  True

This customized cache chooser will use the `my_cache` for the `pow`
function, and use the `global_cache` for all other functions:

  >>> my_cache = ram.RAMCache()
  >>> def my_choose_cache(fun_name):
  ...     if fun_name.endswith('.pow'):
  ...         return RAMCacheAdapter(my_cache)
  ...     else:
  ...         return RAMCacheAdapter(global_cache)
  >>> interface.directlyProvides(my_choose_cache, ICacheChooser)
  >>> sm.registerUtility(my_choose_cache)

Both caches are empty at this point:

  >>> len(global_cache.getStatistics())
  0
  >>> len(my_cache.getStatistics())
  0

Let's fill them:

  >>> pow(3, 2)
  Someone or something called me
  9
  >>> pow(3, 2)
  9
  >>> len(global_cache.getStatistics())
  0
  >>> len(my_cache.getStatistics())
  1

  >>> add(3, 2)
  Someone or something called me
  5
  >>> add(3, 2)
  5
  >>> len(global_cache.getStatistics())
  1
  >>> len(my_cache.getStatistics())
  1

Function Documentation

def plone.memoize.ram.cache (   get_key)

Definition at line 175 of file ram.py.

00175 
00176 def cache(get_key):
00177     return volatile.cache(get_key, get_cache=store_in_cache)

Here is the caller graph for this function:

def plone.memoize.ram.choose_cache (   fun_name)

Definition at line 162 of file ram.py.

00162 
00163 def choose_cache(fun_name):
00164     return RAMCacheAdapter(component.queryUtility(IRAMCache),
00165                            globalkey=fun_name)
00166 interface.directlyProvides(choose_cache, ICacheChooser)

def plone.memoize.ram.store_in_cache (   fun,
  args,
  kwargs 
)

Definition at line 167 of file ram.py.

00167 
00168 def store_in_cache(fun, *args, **kwargs):
00169     key = '%s.%s' % (fun.__module__, fun.__name__)
00170     cache_chooser = component.queryUtility(ICacheChooser)
00171     if cache_chooser is not None:
00172         return cache_chooser(key)
00173     else:
00174         return RAMCacheAdapter(global_cache, globalkey=key)


Variable Documentation

Definition at line 111 of file ram.py.

tuple plone.memoize.ram.global_cache = ram.RAMCache()

Definition at line 108 of file ram.py.

tuple plone.memoize.ram.MARKER = object()

Definition at line 112 of file ram.py.