Skip to main content

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

Project description

Pipeline status Test code coverage PyPI RTFD

HermesCache

Hermes is a Python caching library. It was designed to fulfil the following requirements:

  • Tag-based O(1) cache invalidation
  • Dogpile effect (cache stampede) mitigation
  • Support for multi-threaded, multi-process and multi-machine operation
  • Cache compression
  • Modular design (pluggable backends, compressors, serialisers, etc.)
  • Simple yet flexible decorator API

Implemented backends: redis, memcached, dict.

Note

Hermes 0.8+ is Python 3-only. Previous versions supported Python 2.

Installation

pip install HermesCache

For Redis and Memcached it has the following extra dependencies.

HermesCache[redis] Pure Python Redis client
HermesCache[redis-ext] Pure Python Redis client & C extension parser
HermesCache[memcached] Pure Python Memcached client
HermesCache[memcached-ext] C extension Memcached client

Quickstart

The following demonstrates the most of the package’s 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, a, b: f'avg:{a}:{b}')
  def baz(self, a, b):
    return (a + b) / 2

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

Note

The API encourages import-time instantiation of Hermes facade to allow decoration of existing classes and functions, to make caching transparent to them. The instantiation has no side-effects. Underlying backend connections are lazy.

Moreover, if backend configuration is only available at runtime, Hermes.backend instance can be replaced at runtime.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for HermesCache, version 0.9.0
Filename, size File type Python version Upload date Hashes
Filename, size HermesCache-0.9.0.tar.gz (21.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page