cache.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Cement core cache module."""
  2. from ..core import interface, handler
  3. from ..utils.misc import minimal_logger
  4. LOG = minimal_logger(__name__)
  5. def cache_validator(klass, obj):
  6. """Validates a handler implementation against the ICache interface."""
  7. members = [
  8. '_setup',
  9. 'get',
  10. 'set',
  11. 'delete',
  12. 'purge',
  13. ]
  14. interface.validate(ICache, obj, members)
  15. class ICache(interface.Interface):
  16. """
  17. This class defines the Cache Handler Interface. Classes that
  18. implement this handler must provide the methods and attributes defined
  19. below.
  20. Implementations do *not* subclass from interfaces.
  21. Usage:
  22. .. code-block:: python
  23. from cement.core import cache
  24. class MyCacheHandler(object):
  25. class Meta:
  26. interface = cache.ICache
  27. label = 'my_cache_handler'
  28. ...
  29. """
  30. # pylint: disable=W0232, C0111, R0903
  31. class IMeta:
  32. """Interface meta-data."""
  33. label = 'cache'
  34. """The label (or type identifier) of the interface."""
  35. validator = cache_validator
  36. """Interface validator function."""
  37. # Must be provided by the implementation
  38. Meta = interface.Attribute('Handler meta-data')
  39. def _setup(app_obj):
  40. """
  41. The _setup function is called during application initialization and
  42. must 'setup' the handler object making it ready for the framework
  43. or the application to make further calls to it.
  44. :param app_obj: The application object.
  45. :returns: ``None``
  46. """
  47. def get(key, fallback=None):
  48. """
  49. Get the value for a key in the cache. If the key does not exist
  50. or the key/value in cache is expired, this functions must return
  51. 'fallback' (which in turn must default to None).
  52. :param key: The key of the value stored in cache
  53. :param fallback: Optional value that is returned if the cache is
  54. expired or the key does not exist. Default: None
  55. :returns: Unknown (whatever the value is in cache, or the `fallback`)
  56. """
  57. def set(key, value, time=None):
  58. """
  59. Set the key/value in the cache for a set amount of `time`.
  60. :param key: The key of the value to store in cache.
  61. :param value: The value of that key to store in cache.
  62. :param time: A one-off expire time. If no time is given, then a
  63. default value is used (determined by the implementation).
  64. :type time: ``int`` (seconds) or ``None``
  65. :returns: ``None``
  66. """
  67. def delete(key):
  68. """
  69. Deletes a key/value from the cache.
  70. :param key: The key in the cache to delete.
  71. :returns: True if the key is successfully deleted, False otherwise.
  72. :rtype: ``boolean``
  73. """
  74. # pylint: disable=E0211
  75. def purge():
  76. """
  77. Clears all data from the cache.
  78. """
  79. class CementCacheHandler(handler.CementBaseHandler):
  80. """
  81. Base class that all Cache Handlers should sub-class from.
  82. """
  83. class Meta:
  84. """
  85. Handler meta-data (can be passed as keyword arguments to the parent
  86. class).
  87. """
  88. label = None
  89. """String identifier of this handler implementation."""
  90. interface = ICache
  91. """The interface that this handler class implements."""
  92. def __init__(self, *args, **kw):
  93. super(CementCacheHandler, self).__init__(*args, **kw)