wikidata.cache — Caching policies

Changed in version 0.5.0.


The type of keys to look up cached values. Alias of str.

class wikidata.cache.CachePolicy

Interface for caching policies.

get(key: <function NewType.<locals>.new_type at 0x7f63ceb238c8>) → typing.Union[<function NewType.<locals>.new_type at 0x7f63ceb239d8>, NoneType]

Look up a cached value by its key.

Parameters:key (CacheKey) – The key string to look up a cached value.
Returns:The cached value if it exists. None if there’s no such key.
Return type:Optional[CacheValue]
set(key: <function NewType.<locals>.new_type at 0x7f63ceb238c8>, value: typing.Union[<function NewType.<locals>.new_type at 0x7f63ceb239d8>, NoneType]) → None

Create or update a cache.

  • key (CacheKey) – A key string to create or update.
  • value (Optional[CacheValue]) – A value to cache. None to remove cache.

The type of cached values.

class wikidata.cache.MemoryCachePolicy(max_size: int = 128) → None

LRU (least recently used) cache in memory.

Parameters:max_size (int) – The maximum number of values to cache. 128 by default.
class wikidata.cache.NullCachePolicy

No-op cache policy.

class wikidata.cache.ProxyCachePolicy(cache_object, timeout: int, property_timeout: typing.Union[int, NoneType] = None, namespace: str = 'wd_') → None

This proxy policy is a proxy or an adaptor to another cache object. Cache objects can be anything if they satisfy the following interface:

def get(key: str) -> Optional[bytes]: pass
def set(key: str, value: bytes, timeout: int=0) -> None: pass
def delete(key: str) -> None: pass

(The above methods omit self parameters.) It’s compatible with de facto interface for caching libraries in Python (e.g. python-memcached, werkzeug.contrib.cache).

  • cache_object – The cache object to adapt. Read the above explanation.
  • timeout (int) – Lifespan of every cache in seconds. 0 means no expiration.
  • property_timeout (int) – Lifespan of caches for properties (in seconds). Since properties don’t change frequently or their changes usually don’t make important effect, longer lifespan of properties’ cache can be useful. 0 means no expiration. Set to the same as timeout by default.
  • namespace (str) – The common prefix attached to every cache key. 'wd_' by default.