memoize package

Submodules

memoize.coerced module

[Internal use only] Some functions work differently way depending if asyncio or Tornado is used - this is resolved here.

memoize.configuration module

[API] Provides interface (and built-in implementations) of full cache configuration.

class memoize.configuration.CacheConfiguration[source]

Bases: object

Provides configuration for cache.

configured() → bool[source]

Cache will raise NotConfiguredCacheCalledException if this returns false. May be useful if when cache is reconfigured in runtime.

entry_builder() → memoize.entrybuilder.CacheEntryBuilder[source]

Determines which CacheEntryBuilder is to be used by cache.

eviction_strategy() → memoize.eviction.EvictionStrategy[source]

Determines which EvictionStrategy is to be used by cache.

key_extractor() → memoize.key.KeyExtractor[source]

Determines which KeyExtractor is to be used by cache.

method_timeout() → datetime.timedelta[source]

Defines how much time wrapped method can take to complete.

storage() → memoize.storage.CacheStorage[source]

Determines which CacheStorage is to be used by cache.

class memoize.configuration.DefaultInMemoryCacheConfiguration[source]

Bases: memoize.configuration.CacheConfiguration

Default parameters that describe in-memory cache. Be ware that parameters used do not suit every case.

configured() → bool[source]

Cache will raise NotConfiguredCacheCalledException if this returns false. May be useful if when cache is reconfigured in runtime.

entry_builder() → memoize.entrybuilder.ProvidedLifeSpanCacheEntryBuilder[source]

Determines which CacheEntryBuilder is to be used by cache.

eviction_strategy() → memoize.eviction.LeastRecentlyUpdatedEvictionStrategy[source]

Determines which EvictionStrategy is to be used by cache.

key_extractor() → memoize.key.EncodedMethodReferenceAndArgsKeyExtractor[source]

Determines which KeyExtractor is to be used by cache.

method_timeout() → datetime.timedelta[source]

Defines how much time wrapped method can take to complete.

storage() → memoize.storage.LocalInMemoryCacheStorage[source]

Determines which CacheStorage is to be used by cache.

class memoize.configuration.MutableCacheConfiguration(configured: bool, storage: memoize.storage.CacheStorage, key_extractor: memoize.key.KeyExtractor, eviction_strategy: memoize.eviction.EvictionStrategy, entry_builder: memoize.entrybuilder.CacheEntryBuilder, method_timeout: datetime.timedelta)[source]

Bases: memoize.configuration.CacheConfiguration

Mutable configuration which can be change at runtime. May be also used to customize existing configuration (for example a default one, which is immutable).

configured() → bool[source]

Cache will raise NotConfiguredCacheCalledException if this returns false. May be useful if when cache is reconfigured in runtime.

entry_builder() → memoize.entrybuilder.CacheEntryBuilder[source]

Determines which CacheEntryBuilder is to be used by cache.

eviction_strategy() → memoize.eviction.EvictionStrategy[source]

Determines which EvictionStrategy is to be used by cache.

static initialized_with(configuration: memoize.configuration.CacheConfiguration) → memoize.configuration.MutableCacheConfiguration[source]
key_extractor() → memoize.key.KeyExtractor[source]

Determines which KeyExtractor is to be used by cache.

method_timeout() → datetime.timedelta[source]

Defines how much time wrapped method can take to complete.

set_configured(value: bool) → memoize.configuration.MutableCacheConfiguration[source]
set_entry_builder(value: memoize.entrybuilder.CacheEntryBuilder) → memoize.configuration.MutableCacheConfiguration[source]
set_eviction_strategy(value: memoize.eviction.EvictionStrategy) → memoize.configuration.MutableCacheConfiguration[source]
set_key_extractor(value: memoize.key.KeyExtractor) → memoize.configuration.MutableCacheConfiguration[source]
set_method_timeout(value: datetime.timedelta) → memoize.configuration.MutableCacheConfiguration[source]
set_storage(value: memoize.storage.CacheStorage) → memoize.configuration.MutableCacheConfiguration[source]
storage() → memoize.storage.CacheStorage[source]

Determines which CacheStorage is to be used by cache.

exception memoize.configuration.NotConfiguredCacheCalledException[source]

Bases: Exception

memoize.entry module

[Internal use only] Contains implementation of cache entry.

class memoize.entry.CacheEntry(created: datetime.datetime, update_after: datetime.datetime, expires_after: datetime.datetime, value: Any)[source]

Bases: object

Implementation of cache entry used internally

memoize.entrybuilder module

[API] Provides interface (and built-in implementations) how cache entries should be constructed (responsibility of expiry & update after times lies here). This interface is used in cache configuration.

class memoize.entrybuilder.CacheEntryBuilder[source]

Bases: object

build(key: str, value: Any) → memoize.entry.CacheEntry[source]

Constructs cache entry object (sets creation time, can transform value, governs update/expiration times).

class memoize.entrybuilder.ProvidedLifeSpanCacheEntryBuilder(update_after: datetime.timedelta = datetime.timedelta(seconds=600), expire_after: datetime.timedelta = datetime.timedelta(seconds=1800))[source]

Bases: memoize.entrybuilder.CacheEntryBuilder

CacheEntryBuilder which uses constant delays independent form values that are cached

build(key: str, value: Any) → memoize.entry.CacheEntry[source]

Constructs cache entry object (sets creation time, can transform value, governs update/expiration times).

update_timeouts(update_after: datetime.timedelta, expire_after: datetime.timedelta) → None[source]

memoize.eviction module

[API] Provides interface (and built-in implementations) how cache entries should be constructed (responsibility of expiry & update after times lies here). This interface is used in cache configuration.

class memoize.eviction.EvictionStrategy[source]

Bases: object

mark_read(key: str) → None[source]

Informs strategy that entry related to given key was read by current client.

mark_released(key: str) → None[source]

Informs strategy that entry related to given key was deemed non-essential by current client.

mark_written(key: str, entry: memoize.entry.CacheEntry) → None[source]

Informs strategy that entry related to given key was updated by current client.

next_to_release() → Optional[str][source]

Returns element that should be released by the current client according to this strategy (or None).

class memoize.eviction.LeastRecentlyUpdatedEvictionStrategy(capacity=4096)[source]

Bases: memoize.eviction.EvictionStrategy

mark_read(key: str) → None[source]

Informs strategy that entry related to given key was read by current client.

mark_released(key: str) → None[source]

Informs strategy that entry related to given key was deemed non-essential by current client.

mark_written(key: str, entry: memoize.entry.CacheEntry) → None[source]

Informs strategy that entry related to given key was updated by current client.

next_to_release() → Optional[str][source]

Returns element that should be released by the current client according to this strategy (or None).

class memoize.eviction.NoEvictionStrategy[source]

Bases: memoize.eviction.EvictionStrategy

Strategy to be used when delegating eviction to cache itself. This strategy performs no actions.

mark_read(key: str) → None[source]

Informs strategy that entry related to given key was read by current client.

mark_released(key: str) → None[source]

Informs strategy that entry related to given key was deemed non-essential by current client.

mark_written(key: str, entry: memoize.entry.CacheEntry) → None[source]

Informs strategy that entry related to given key was updated by current client.

next_to_release() → Optional[str][source]

Returns element that should be released by the current client according to this strategy (or None).

memoize.exceptions module

[API] Contains exceptions that may be exposed to the library client.

exception memoize.exceptions.CachedMethodFailedException[source]

Bases: Exception

memoize.key module

[API] Provides interface (and built-in implementations) how cache keys are constructed. This interface is used in cache configuration.

class memoize.key.EncodedMethodNameAndArgsKeyExtractor(skip_first_arg_as_self=False)[source]

Bases: memoize.key.KeyExtractor

Encodes method name, args & kwargs to string and uses that as cache entry key. This KeyExtractor is class-centric and creates same keys for all objects of the same type.

Note: If wrapped function is a method (has ‘self’ as first positional arg) you may want to exclude ‘self’ from key by setting ‘skip_first_arg_as_self’ flag. For static methods of ordinary functions flag should be set to ‘False’.

Warning: uses method name only, so be cautious and do not wrap methods of different classes with the same names while using same store and ‘skip_first_arg_as_self’ set to False.

format_key(method_reference, call_args: Tuple[Any, ...], call_kwargs: Dict[str, Any]) → str[source]

Using wrapped method object, call args and call keyword args, prepare cache entry key.

class memoize.key.EncodedMethodReferenceAndArgsKeyExtractor[source]

Bases: memoize.key.KeyExtractor

Encodes method reference, args & kwargs to string and uses that as cache entry key. This KeyExtractor is object-centric and creates different keys for different objects of the same type (so when you create new objects - for instance after app restart - old entries in external store like Redis will be unreachable).

format_key(method_reference, call_args: Tuple[Any, ...], call_kwargs: Dict[str, Any]) → str[source]

Using wrapped method object, call args and call keyword args, prepare cache entry key.

class memoize.key.KeyExtractor[source]

Bases: object

Provides logic of cache key construction.

format_key(method_reference, call_args: Tuple[Any, ...], call_kwargs: Dict[str, Any]) → str[source]

Using wrapped method object, call args and call keyword args, prepare cache entry key.

memoize.memoize_configuration module

[API] Provides global config of the library. Translates environment variables into values used internally by the library.

memoize.serde module

[API] Provides interface (and built-in implementations) of SerDe that may be used to implement cache storage.

class memoize.serde.EncodingSerDe(serde: memoize.serde.SerDe, binary_encoding: str = 'zip')[source]

Bases: memoize.serde.SerDe

Applies extra encoding to the data (for instance compression when ‘zip’ or ‘bz2’ codec used).

deserialize(value: bytes) → memoize.entry.CacheEntry[source]
serialize(value: memoize.entry.CacheEntry) → bytes[source]
class memoize.serde.JsonSerDe(string_encoding: str = 'utf-8', value_to_reversible_repr: Callable[[Any], Any] = <function JsonSerDe.<lambda>>, reversible_repr_to_value: Callable[[Any], Any] = <function JsonSerDe.<lambda>>)[source]

Bases: memoize.serde.SerDe

Uses encoded json string as binary representation. Value of cached type should consist of types which are json-reversible (json.loads(json.dumps(v)) is equal to v) or one should provide (by constructor) functions converting values to/from such representation.

deserialize(data: bytes) → memoize.entry.CacheEntry[source]
serialize(entry: memoize.entry.CacheEntry) → bytes[source]
class memoize.serde.PickleSerDe(pickle_protocol=4)[source]

Bases: memoize.serde.SerDe

Uses encoded pickles as binary representation.

deserialize(data: bytes) → memoize.entry.CacheEntry[source]
serialize(entry: memoize.entry.CacheEntry) → bytes[source]
class memoize.serde.SerDe[source]

Bases: object

Responsible for (de)serialization of values handled by CacheStorage (if SerDes are supported).

deserialize(data: bytes) → memoize.entry.CacheEntry[source]
serialize(entry: memoize.entry.CacheEntry) → bytes[source]

memoize.statuses module

memoize.storage module

[API] Provides interface (and built-in implementations) of storage for cache entries. This interface is used in cache configuration.

class memoize.storage.CacheStorage[source]

Bases: object

get(key: str) → Optional[memoize.entry.CacheEntry][source]

Request value for given key. If currently there is no such value, returns None. Has to be async.

offer(key: str, entry: memoize.entry.CacheEntry) → None[source]

Offer entry to be stored. If storage already has more relevant data, offer may be declined. Has to be async.

release(key: str) → None[source]

Declare that current client does not need entry determined by given key. Has to be async.

class memoize.storage.LocalInMemoryCacheStorage[source]

Bases: memoize.storage.CacheStorage

Implementation that stores all entries as-is in a dictionary residing solely in memory.

get(key: str) → Optional[memoize.entry.CacheEntry][source]

Request value for given key. If currently there is no such value, returns None. Has to be async.

offer(key: str, entry: memoize.entry.CacheEntry) → None[source]

Offer entry to be stored. If storage already has more relevant data, offer may be declined. Has to be async.

release(key: str) → None[source]

Declare that current client does not need entry determined by given key. Has to be async.

memoize.wrapper module

Module contents