pip_services3_components.cache package

Submodules

Module contents

pip_services3_components.cache.__init__

Abstract implementation of various distributed caches. We can save an object to cache and retrieve it object by its key, using various implementations.

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_components.cache.CacheEntry(key: str, value: str, timeout: int)

Bases: object

Data object to store cached values with their keys used by MemoryCache

get_expiration()int

Gets the expiration timeout.

Returns

the expiration timeout in milliseconds.

get_key()str

Gets the key to locate the cached value.

Returns

the value key.

get_value() → Any

Gets the cached value.

Returns

the value object.

is_expired()bool

Checks if this value already expired.

Returns

true if the value already expires and false otherwise.

set_value(value: Any, timeout: int)

Sets a new value and extends its expiration.

Parameters
  • value – a new cached value.

  • timeout – a expiration timeout in milliseconds.

class pip_services3_components.cache.DefaultCacheFactory

Bases: pip_services3_components.build.Factory.Factory

Creates ICache components by their descriptors.

See Factory, ICache, MemoryCache, NullCache

MemoryCacheDescriptor = <pip_services3_commons.refer.Descriptor.Descriptor object>
NullCacheDescriptor = <pip_services3_commons.refer.Descriptor.Descriptor object>
descriptor = <pip_services3_commons.refer.Descriptor.Descriptor object>
class pip_services3_components.cache.ICache

Bases: abc.ABC

Interface for caches that are used to cache values to improve performance.

remove(correlation_id: Optional[str], key: str)

Removes a value from the cache by its key.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

retrieve(correlation_id: Optional[str], key: str) → Any

Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns None.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

Returns

a cached value or None if value wasn’t found or timeout expired.

store(correlation_id: Optional[str], key: str, value: Any, timeout: int) → Any

Stores value in the cache with expiration time.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

  • value – a value to store.

  • timeout – expiration timeout in milliseconds.

Returns

a cached value stored in the cache.

class pip_services3_components.cache.MemoryCache

Bases: pip_services3_components.cache.ICache.ICache, pip_services3_commons.config.IReconfigurable.IReconfigurable, pip_services3_commons.run.ICleanable.ICleanable

Cache that stores values in the process memory.

Remember: This implementation is not suitable for synchronization of distributed processes.

### Configuration parameters ### options:

  • timeout: default caching timeout in milliseconds (default: 1 minute)

  • max_size: maximum number of values stored in this cache (default: 1000)

Example:

cache = MemoryCache()
cache.store("123", "key1", "ABC", 0)
clear(correlation_id: Optional[str])

Clears component state.

Parameters

correlation_id – (optional) transaction id to trace execution through call chain.

configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)

Configures component by passing configuration parameters.

Parameters

config – configuration parameters to be set.

remove(correlation_id: Optional[str], key: str)

Removes a value from the cache by its key.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

retrieve(correlation_id: Optional[str], key: str) → Any

Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns None.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

Returns

a cached value or None if value wasn’t found or timeout expired.

store(correlation_id: Optional[str], key: str, value: Any, timeout: int) → Any

Stores value in the cache with expiration time.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

  • value – a value to store.

  • timeout – expiration timeout in milliseconds.

Returns

a cached value stored in the cache.

class pip_services3_components.cache.NullCache

Bases: pip_services3_components.cache.ICache.ICache

Dummy cache implementation that doesn’t do anything.

It can be used in testing or in situations when cache is required but shall be disabled.

remove(correlation_id: Optional[str], key: str)

Removes a value from the cache by its key.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

retrieve(correlation_id: Optional[str], key: str) → Any

Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns None.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

Returns

a cached value or None if value wasn’t found or timeout expired.

store(correlation_id: Optional[str], key: str, value: Any, timeout: int) → Any

Stores value in the cache with expiration time.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • key – a unique value key.

  • value – a value to store.

  • timeout – expiration timeout in milliseconds.

Returns

a cached value stored in the cache.