/ Caching

Python caching library with tag-based invalidation and dogpile effect prevention

Python caching library with tag-based invalidation and dogpile effect prevention

HermesCache

Hermes is a Python caching library. The requirements it was designed to fulfil:

  • Tag-based cache invalidation
  • Dogpile effect prevention
  • Thread-safety
  • Straightforward design
  • Simple, at the same time, flexible decorator as end-user API
  • Interface for implementing multiple backends
  • Implemented backends: redis, memcached, dict (no expiry).

Install

Make sure desired backend client library is installed. See below.

Usage

The following demonstrates all end-user API.

import hermes.backend.redis

cache = hermes.Hermes(hermes.backend.redis.Backend, ttl = 600, host = 'localhost', db = 1)


@cache
def foo(a, b):
  return a * b

class Example:

  @cache(tags = ('math', 'power'), ttl = 1200)
  def bar(self, a, b):
    return a ** b

  @cache(tags = ('math', 'avg'), key = lambda fn, *args, **kwargs: 'avg:{0}:{1}'.format(*args))
  def baz(self, a, b):
    return (a + b) / 2.0


print(foo(2, 333))

example = Example()
print(example.bar(2, 10))
print(example.baz(2, 10))

foo.invalidate(2, 333)
example.bar.invalidate(2, 10)
example.baz.invalidate(2, 10)

cache.clean(['math']) # invalidate entries tagged 'math'
cache.clean()         # flush cache