1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Storm/memcached implementation of lazr.restful's representation cache."""
8
from zope.component import getUtility
9
from zope.security.proxy import removeSecurityProxy
10
from zope.traversing.browser import absoluteURL
12
from canonical.config import config
13
from lp.services.memcache.interfaces import IMemcacheClient
14
from lazr.restful.simple import BaseRepresentationCache
15
from lazr.restful.utils import get_current_web_service_request
19
'MemcachedStormRepresentationCache',
23
class MemcachedStormRepresentationCache(BaseRepresentationCache):
24
"""Caches lazr.restful representations of Storm objects in memcached."""
27
"""Initialize with the memcached client."""
28
self.client = getUtility(IMemcacheClient)
30
def key_for(self, obj, media_type, version):
31
"""See `BaseRepresentationCache`."""
32
obj = removeSecurityProxy(obj)
34
storm_info = storm.info.get_obj_info(obj)
35
except storm.exceptions.ClassInfoError, e:
36
# There's no Storm data for this object. Don't cache it,
37
# since we don't know how to invalidate the cache.
38
return self.DO_NOT_CACHE
39
table_name = storm_info.cls_info.table
40
primary_key = tuple(var.get() for var in storm_info.primary_vars)
41
identifier = table_name + repr(primary_key)
44
+ ',' + config._instance_name
45
+ ',' + media_type + ',' + str(version)).replace(' ', '.')
48
def get_by_key(self, key, default=None):
49
"""See `BaseRepresentationCache`."""
50
value = self.client.get(key)
55
def set_by_key(self, key, value):
56
"""See `BaseRepresentationCache`."""
59
time=config.vhost.api.representation_cache_expiration_time)
61
def delete_by_key(self, key):
62
"""See `BaseRepresentationCache`."""
63
self.client.delete(key)