Description
How to use annotation design pattern to store arbitary values on Python objects (Plone site, HTTP request) for storage and caching purposes.
Annotations is conflict-free way to stick attributes on arbitary Python objects.
Plone uses annotations for
See zope.annotation package <http://pypi.python.org/pypi/zope.annotation/3.4.1>.
Store cached values on HTTP request during the life cycle of one request processing. This allows you to cache computed values if the computation function is called from the different, unrelated, code paths.
from zope.annotation.interfaces import IAnnotations
# Non-conflicting key
KEY = "mypackage.something"
annotations = IAnnotations(request)
value = annotations.get(KEY, None)
if value is None:
# Compute value and store it on request object for further look-ups
value = annotations[KEY] = something()