← Back to team overview

launchpad-dev team mailing list archive

Alternative/replacement API for cached properties.

 

The API (and implementation) in canonical.cachedproperty has been
getting a bit hairy in order to support the cache invalidation work
that Rob has been doing.

I've had a idea rattling around for a while to make a nicer cached
property API, so I started an experiment... and it turned out well, I
think.

Henning has reviewed it favourably - http://tinyurl.com/385e6r7 - but
he asked that I get some feedback before it lands (or to prevent it
from landing).

Some key points:

- Cached properties are declared using a cachedproperty decorator, so
  most code will not require any modification.

- This first branch allows both canonical.cachedproperty and
  lp.services.propertycache to co-exist. A subsequent branch will
  deprecate or remove canonical.cachedproperty.

- The cached values are stored in a separate cache object, obtained by
  adapting the target object to IPropertyCache:

    >>> cache_for_model_object = IPropertyCache(model_object)
    >>> cache_for_model_object.a_cached_property = 1234

  It behaves mostly like a regular instance, but has three extra
  features for convenience:

  * The cache can be iterated over to get the names of currently
    cached values.

      >>> list(cache_for_model_object)
      ['a_cached_property']

  * The cache responds to membership tests.

      >>> 'a_cached_property' in cache_for_model_object
      True

  * If something is not found in the cache, deleting it is a no-op
    rather than an error.

      >>> del cache_for_model_object.a_cached_property
      >>> del cache_for_model_object.a_cached_property

- The cache can be cleared using an IPropertyCacheManager:

    >>> IPropertyCacheManager(cache_for_model_object).clear()

  or, more simply:

    >>> IPropertyCacheManager(model_object).clear()

That's about it. It's pretty simple and unsurprising to use. See
lib/lp/services/doc/propertycache.txt for the full documentation.

Gavin.