wikidata.cache — Caching policies

Changed in version 0.5.0.

wikidata.cache.CacheKey(x)

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

class wikidata.cache.CachePolicy

Interface for caching policies.

get(key: NewType.<locals>.new_type) → Optional[NewType.<locals>.new_type]

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: NewType.<locals>.new_type, value: Optional[NewType.<locals>.new_type]) → None

Create or update a cache.

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

The type of cached values.

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

LRU (least recently used) cache in memory.

Parameters:max_size (int) – The maximum number of values to cache. 128 by default.
get(key: NewType.<locals>.new_type) → Optional[NewType.<locals>.new_type]

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: NewType.<locals>.new_type, value: Optional[NewType.<locals>.new_type]) → None

Create or update a cache.

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

No-op cache policy.

get(key: NewType.<locals>.new_type) → Optional[NewType.<locals>.new_type]

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: NewType.<locals>.new_type, value: Optional[NewType.<locals>.new_type]) → None

Create or update a cache.

Parameters:
  • key (CacheKey) – A key string to create or update.
  • value (Optional[CacheValue]) – A value to cache. None to remove cache.
class wikidata.cache.ProxyCachePolicy(cache_object, timeout: int, property_timeout: Optional[int] = None, namespace: str = 'wd_')

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).

Parameters:
  • 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.
get(key: NewType.<locals>.new_type) → Optional[NewType.<locals>.new_type]

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: NewType.<locals>.new_type, value: Optional[NewType.<locals>.new_type]) → None

Create or update a cache.

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