Back to index

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

Classes

class  RequestMemo

Functions

def store_in_annotation_of
def cache

Variables

tuple _marker = object()
 memoize_diy_request = RequestMemo
tuple __all__ = (memoize_diy_request, store_in_annotation_of, cache)

Detailed Description

Memoize decorator for methods.

Stores values in an annotation of the request.

Function Documentation

def plone.memoize.request.cache (   get_key,
  get_request = 'request' 
)

Definition at line 59 of file request.py.

00059 
00060 def cache(get_key, get_request='request'):
00061     r"""
00062     This is a hypothetical function `increment` that'll store the
00063     cache value on `a.request`, where a is the only argument to the
00064     function:
00065     
00066       >>> def increment(a):
00067       ...     print 'Someone or something called me'
00068       ...     return a + 1
00069 
00070     Now we need to define this `a`.  For this, we'll inherit from
00071     `int` and add a `request` class variable.  Note that we also make
00072     our fake request `IAttributeAnnotatable`, because that's how the
00073     cache values are stored on the request:
00074 
00075       >>> from zope.publisher.browser import TestRequest
00076       >>> class A(int):
00077       ...     request = TestRequest()
00078       >>> from zope.interface import directlyProvides
00079       >>> try:
00080       ...     from zope.annotation.interfaces import IAttributeAnnotatable
00081       ... except ImportError:
00082       ...     from zope.app.annotation.interfaces import IAttributeAnnotatable
00083       >>> directlyProvides(A.request, IAttributeAnnotatable)
00084 
00085     In addition to this request, we'll also need to set up a cache key
00086     generator.  We'll use the integer value of the only argument for
00087     that:
00088 
00089       >>> get_key = lambda fun, a, *args: a
00090 
00091     Let's decorate our `increment` function now with the `cache`
00092     decorator.  We'll tell the decorator to use `args_hash` for
00093     generating the key. `get_request` will tell the decorator how to
00094     actually find the `request` in the variable scope of the function
00095     itself:
00096     
00097       >>> cached_increment = \
00098       ...     cache(get_key=get_key, get_request='a.request')(increment)
00099 
00100       >>> cached_increment(A(1))
00101       Someone or something called me
00102       2
00103       >>> cached_increment(A(1))
00104       2
00105       >>> IAnnotations(A.request)
00106       {'plone.memoize.request.increment:1': 2}
00107 
00108     If `request` is already part of the function's argument list, we
00109     don't need to specify any expression:
00110 
00111       >>> @cache(get_key=get_key)
00112       ... def increment_plus(a, request):
00113       ...     print 'Someone or something called me'
00114       ...     return a + 1
00115 
00116       >>> increment_plus(42, A.request)
00117       Someone or something called me
00118       43
00119       >>> increment_plus(42, A.request)
00120       43
00121       >>> IAnnotations(A.request)['plone.memoize.request.increment_plus:42']
00122       43
00123     """
00124 
00125     return volatile.cache(get_key,
00126                           get_cache=store_in_annotation_of(get_request))

Here is the call graph for this function:

Definition at line 46 of file request.py.

00046 
00047 def store_in_annotation_of(expr):
00048     def _store_in_annotation(fun, *args, **kwargs):
00049         # Use expr to find out the name of the request variable
00050         vars = {}
00051         for index, name in enumerate(inspect.getargspec(fun)[0]):
00052             if name in kwargs:
00053                 vars[name] = kwargs[name]
00054             else:
00055                 vars[name] = args[index]
00056         request = eval(expr, {}, vars)
00057         return IAnnotations(request)
00058     return _store_in_annotation

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 129 of file request.py.

Definition at line 16 of file request.py.

Definition at line 127 of file request.py.