Customisation

The following classes and delegates allow customisation of different details of how Hermes works, including:

  • hash algorithm

    Mangler()

    Key manager responsible for creating keys, hashing and serialisation.

    class CustomMangler(hermes.Mangler):
    
      def hash(self, value):
        return str(hash(value))
    
    cache = hermes.Hermes(
      hermes.backend.redis.Backend, mangler = CustomMangler()
    )
    
  • serialisation algorithm

    Serialiser(dumps, loads)

    Serialisation delegate.

    cache = hermes.Hermes(hermes.backend.redis.Backend)
    
    # use another serialisation algorithm
    cache.mangler.serialiser = hermes.Serialiser(
      msgpack.dumps, lambda b: msgpack.loads(b, strict_map_key = False)
    )
    
  • compression algorithm

    Compressor(compress, decompress, decompressError)

    Compression delegate.

    cache = hermes.Hermes(hermes.backend.redis.Backend)
    
    # disable compression
    cache.mangler.compressor = None
    
    # use another compression algorithm
    cache.mangler.compressor = hermes.Compressor(
      snappy.compress, snappy.decompress, snappy.UncompressError, 32
    )
    
  • interpretation of cache operation failure (e.g. ignoring backend errors when the backend is down and calling the cached callable instead)

    Cached(frontend, callable, *[, ttl, key, tags])

    Cache-point wrapper for callables and descriptors.

    CachedCoro(frontend, callable, *[, ttl, ...])

    Cache-point wrapper for coroutine functions.

    class CustomCached(hermes.Cached):
    
      def __call__(self, *args, **kwargs):
        try:
          return super().__call__(*args, **kwargs)
        except redis.RedisError:
          return self._callable(*args, **kwargs)
    
    cache = hermes.Hermes(
      hermes.backend.redis.Backend, cachedcls = CustomCached
    )