redis_func_cache.mixins.hash module#

class redis_func_cache.mixins.hash.AbstractHashMixin[source]#

Bases: ABC

An abstract mixin class for hash function name, source code, and arguments.

Inheritance diagram of AbstractHashMixin

The hash result is used inside the redis (ordered) set and hash map in redis, aka the sub-key.

Do NOT use the mixin class directly. Inherit it and override the __hash_config__ to define the algorithm and serializer.

Example

class JsonMd5B64HashMixin(AbstractHashMixin):
    __hash_config__ = HashConfig(
        algorithm="md5",
        serializer=lambda x: json.dumps(x).encode(),
        decoder=lambda x: b64encode(x.digest()),
    )
__hash_config__#

Configure of how to calculate hash for a function.

Type:

HashConfig

calc_hash(f=None, args=None, kwds=None)[source]#

Mixin method to overwrite redis_func_cache.policies.abstract.AbstractPolicy.calc_hash()

All other mixin classes in the module inherit this mixin class, and their hash value are all return by the method.

They use different hash algorithms and serializers defined in the class attribute __hash_config__ to generate different hash value.

Parameters:
  • f (Callable | None) – The function to calculate hash for.

  • args (tuple[Any, ...] | None) – The :term`sequence` arguments of the function.

  • kwds (dict[str, Any] | None) – The keyword arguments of the function.

Return type:

bytes | str | memoryview

Returns:

The hash value of the function.

Raises:

TypeError – If the function is not callable.

class redis_func_cache.mixins.hash.HashConfig(algorithm, serializer, decoder=None, use_bytecode=True)[source]#

Bases: object

A dataclasses.dataclass() Configurator for AbstractHashMixin

Parameters:
  • algorithm (str)

  • serializer (Callable[[Any], bytes])

  • decoder (Optional[Callable[[Hash], KeyT]])

  • use_bytecode (bool)

algorithm: str#

name for hashing algorithm

The name must be supported by hashlib.

decoder: Callable[[Hash], bytes | str | memoryview] | None = None#

function to decode hash digest to member of a sorted/unsorted set and also field name of a hash map in redis.

Default is None, means no decoding and to use the raw digest bytes directly.

serializer: Callable[[Any], bytes]#

function to serialize function positional and keyword arguments.

use_bytecode: bool = True#

whether to use bytecode of the function to calculate hash.

Added in version 0.5.

class redis_func_cache.mixins.hash.JsonMd5Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the MD5 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of JsonMd5Base64HashMixin

class redis_func_cache.mixins.hash.JsonMd5HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the MD5 hash value, and finally returns the digest as bytes.

Inheritance diagram of JsonMd5HashMixin

class redis_func_cache.mixins.hash.JsonMd5HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the MD5 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of JsonMd5HexHashMixin

class redis_func_cache.mixins.hash.JsonSha1Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA1 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of JsonSha1Base64HashMixin

class redis_func_cache.mixins.hash.JsonSha1HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA1 hash value, and finally returns the digest as bytes.

Inheritance diagram of JsonSha1HashMixin

class redis_func_cache.mixins.hash.JsonSha1HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA1 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of JsonSha1HexHashMixin

class redis_func_cache.mixins.hash.JsonSha256Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA256 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of JsonSha256Base64HashMixin

class redis_func_cache.mixins.hash.JsonSha256HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA256 hash value, and finally returns the digest as bytes.

Inheritance diagram of JsonSha256HashMixin

class redis_func_cache.mixins.hash.JsonSha256HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA256 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of JsonSha256HexHashMixin

class redis_func_cache.mixins.hash.JsonSha512Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA512 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of JsonSha512Base64HashMixin

class redis_func_cache.mixins.hash.JsonSha512HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA512 hash value, and finally returns the digest as bytes.

Inheritance diagram of JsonSha512HashMixin

class redis_func_cache.mixins.hash.JsonSha512HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the json module, then calculates the SHA512 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of JsonSha512HexHashMixin

class redis_func_cache.mixins.hash.PickleMd5Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the MD5 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of PickleMd5Base64HashMixin

class redis_func_cache.mixins.hash.PickleMd5HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the MD5 hash value, and finally returns the digest as bytes.

It is the default hash mixin.

Inheritance diagram of PickleMd5HashMixin

class redis_func_cache.mixins.hash.PickleMd5HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the MD5 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of PickleMd5HexHashMixin

class redis_func_cache.mixins.hash.PickleSha1Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA1 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of PickleSha1Base64HashMixin

class redis_func_cache.mixins.hash.PickleSha1HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA1 hash value, and finally returns the digest as bytes.

Inheritance diagram of PickleSha1HashMixin

class redis_func_cache.mixins.hash.PickleSha1HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA1 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of PickleSha1HexHashMixin

class redis_func_cache.mixins.hash.PickleSha256Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA256 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of PickleSha256Base64HashMixin

class redis_func_cache.mixins.hash.PickleSha256HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA256 hash value, and finally returns the digest as bytes.

Inheritance diagram of PickleSha256HashMixin

class redis_func_cache.mixins.hash.PickleSha256HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA256 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of PickleSha256HexHashMixin

class redis_func_cache.mixins.hash.PickleSha512Base64HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA512 hash value, and finally returns the base64 encoded digest.

Inheritance diagram of PickleSha512Base64HashMixin

class redis_func_cache.mixins.hash.PickleSha512HashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA512 hash value, and finally returns the digest as bytes.

Inheritance diagram of PickleSha512HashMixin

class redis_func_cache.mixins.hash.PickleSha512HexHashMixin[source]#

Bases: AbstractHashMixin

Serializes the function name, source code, and arguments using the pickle module, then calculates the SHA512 hash value, and finally returns the hexadecimal representation of the digest.

Inheritance diagram of PickleSha512HexHashMixin