src/pyams_utils/cache.py
branchdev-tf
changeset 408 cf2304af0fab
parent 380 c062ab4db6cd
equal deleted inserted replaced
407:0037199881fb 408:cf2304af0fab
    19 of a given object; this string can be used as a cache key, but also to define an object ID inside
    19 of a given object; this string can be used as a cache key, but also to define an object ID inside
    20 an HTML page.
    20 an HTML page.
    21 A TALES helper extension is also provided to get an object's cache key from a Chameleon template.
    21 A TALES helper extension is also provided to get an object's cache key from a Chameleon template.
    22 """
    22 """
    23 
    23 
    24 __docformat__ = 'restructuredtext'
       
    25 
       
    26 from persistent.interfaces import IPersistent
    24 from persistent.interfaces import IPersistent
    27 from zope.interface import Interface
    25 from zope.interface import Interface
    28 
    26 
    29 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
    27 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
    30 from pyams_utils.interfaces import ICacheKeyValue
    28 from pyams_utils.interfaces import ICacheKeyValue
    31 from pyams_utils.interfaces.tales import ITALESExtension
    29 from pyams_utils.interfaces.tales import ITALESExtension
    32 
    30 
    33 
    31 
       
    32 __docformat__ = 'restructuredtext'
       
    33 
       
    34 
    34 @adapter_config(context=object, provides=ICacheKeyValue)
    35 @adapter_config(context=object, provides=ICacheKeyValue)
    35 def object_cache_key_adapter(obj):
    36 def object_cache_key_adapter(obj):
       
    37     """Cache key adapter for any object"""
    36     return str(id(obj))
    38     return str(id(obj))
    37 
    39 
    38 
    40 
    39 @adapter_config(context=str, provides=ICacheKeyValue)
    41 @adapter_config(context=str, provides=ICacheKeyValue)
    40 def string_cache_key_adapter(obj):
    42 def string_cache_key_adapter(obj):
       
    43     """Cache key adapter for string value"""
    41     return obj
    44     return obj
    42 
    45 
    43 
    46 
    44 @adapter_config(context=IPersistent, provides=ICacheKeyValue)
    47 @adapter_config(context=IPersistent, provides=ICacheKeyValue)
    45 def persistent_cache_key_adapter(obj):
    48 def persistent_cache_key_adapter(obj):
       
    49     """Cache key adapter for persistent object"""
       
    50     # pylint: disable=protected-access
    46     if obj._p_oid:
    51     if obj._p_oid:
    47         return str(int.from_bytes(obj._p_oid, byteorder='big'))
    52         return str(int.from_bytes(obj._p_oid, byteorder='big'))
    48     else:  # unsaved object
    53     return str(id(obj))
    49         return str(id(obj))
       
    50 
    54 
    51 
    55 
    52 @adapter_config(name='cache_key', context=(Interface, Interface, Interface), provides=ITALESExtension)
    56 @adapter_config(name='cache_key', context=(Interface, Interface, Interface),
       
    57                 provides=ITALESExtension)
    53 class CacheKeyTalesExtension(ContextRequestViewAdapter):
    58 class CacheKeyTalesExtension(ContextRequestViewAdapter):
    54     """extension:cache_key(context) TALES extension
    59     """extension:cache_key(context) TALES extension
    55 
    60 
    56     A PyAMS TALES extension which allows to render cache key value for a given context.
    61     A PyAMS TALES extension which allows to render cache key value for a given context.
    57     """
    62     """
    58 
    63 
    59     def render(self, context=None):
    64     def render(self, context=None):
       
    65         """Rendering of TALES extension"""
    60         if context is None:
    66         if context is None:
    61             context = self.request.context
    67             context = self.request.context
    62         return ICacheKeyValue(context)
    68         return ICacheKeyValue(context)