LFU Cache

The lfu module provides the LFUCache (Least Frequently Used) class.

class cacheout.lfu.LFUCache(maxsize=None, ttl=None, timer=None, default=None)[source]

Bases: cacheout.cache.Cache

The Least Frequently Used (LFU) cache is like Cache but uses a least-frequently-used eviction policy.

The primary difference with Cache is that access to cache entries (i.e. calls to get() and set()) are tracked; each call to get() will increment the cache key’s access count while calls to set() will reset the counter. During cache eviction, the entry with the lowest access count is removed first.

add(key, value, ttl=None)

Add cache key/value if it doesn’t already exist. Essentially, this method ignores keys that exist which leaves the original TTL in tact.

Note

Cache key must be hashable.

Parameters:
  • key (mixed) – Cache key to add.
  • value (mixed) – Cache value.
  • ttl (int, optional) – TTL value. Defaults to None which uses ttl.
add_many(items, ttl=None)

Add multiple cache keys at once.

Parameters:items (dict) – Mapping of cache key/values to set.
clear()

Clear all cache entries.

configure(maxsize=None, ttl=None, timer=None, default=None)

Configure cache settings.

This method is meant to support runtime level configurations for global level cache objects.

copy()

Return a copy of the cache.

Returns:OrderedDict
delete(key)

Delete cache key and return number of entries deleted (1 or 0).

Returns:1 if key was deleted, 0 if key didn’t exist.
Return type:int
delete_expired()

Delete expired cache keys and return number of entries deleted.

Returns:Number of entries deleted.
Return type:int
delete_many(iteratee)

Delete multiple cache keys at once filtered by an iteratee that can be one of:

  • list - List of cache keys.
  • str - Search string that supports Unix shell-style wildcards.
  • re.compile() - Compiled regular expression.
  • function - Function that returns whether a key matches. Invoked with iteratee(key).
Parameters:iteratee (list|str|Pattern|callable) – Iteratee to filter by.
Returns:Number of cache keys deleted.
Return type:int
evict()

Perform cache eviction per the cache replacement policy:

  • First, remove all expired entries.
  • Then, remove non-TTL entries using the cache replacement policy.

When removing non-TTL entries, this method will only remove the minimum number of entries to reduce the number of entries below maxsize. If maxsize is 0, then only expired entries will be removed.

Returns:Number of cache entries evicted.
Return type:int
expire_times()

Return cache expirations for TTL keys.

Returns:dict
expired(key, expires_on=None)

Return whether cache key is expired or not.

Parameters:
  • key (mixed) – Cache key.
  • expires_on (float, optional) – Timestamp of when the key is considered expired. Defaults to None which uses the current value returned from timer().
Returns:

bool

full()

Return whether the cache is full or not.

Returns:bool
get(key, default=None)

Return the cache value for key or default or missing(key) if it doesn’t exist or has expired.

Parameters:
  • key (mixed) – Cache key.
  • default (mixed, optional) – Value to return if key doesn’t exist. If any value other than None, then it will take precendence over missing and be used as the return value. If default is callable, it will function like missing and its return value will be set for the cache key. Defaults to None.
Returns:

The cached value.

Return type:

mixed

get_many(iteratee, default=None)

Return many cache values as a dict of key/value pairs filtered by an iteratee that can be one of:

  • list - List of cache keys.
  • str - Search string that supports Unix shell-style wildcards.
  • re.compile() - Compiled regular expression.
  • function - Function that returns whether a key matches. Invoked with iteratee(key).
Parameters:
  • iteratee (list|str|Pattern|callable) – Iteratee to filter by.
  • default (mixed, optional) – Value to return if key doesn’t exist. Defaults to None.
Returns:

dict

has(key)

Return whether cache key exists and hasn’t expired.

Returns:bool
items()

Return a dict_items view of cache items.

Warning

Returned data is copied from the cache object, but any modifications to mutable values will modify this cache object’s data.

Returns:dict_items
keys()

Return dict_keys view of all cache keys.

Note

Cache is copied from the underlying cache storage before returning.

Returns:dict_keys
memoize(*, ttl=None, typed=False)

Decorator that wraps a function with a memoizing callable and works on both synchronous and asynchronous functions.

Each return value from the function will be cached using the function arguments as the cache key. The cache object can be accessed at <function>.cache. The uncached version (i.e. the original function) can be accessed at <function>.uncached. Each return value from the function will be cached using the function arguments as the cache key.

Keyword Arguments:
 
  • ttl (int, optional) – TTL value. Defaults to None which uses ttl.
  • typed (bool, optional) – Whether to cache arguments of a different type separately. For example, <function>(1) and <function>(1.0) would be treated differently. Defaults to False.
popitem()

Delete and return next cache item, (key, value), based on cache replacement policy while ignoring expiration times (i.e. the selection of the item to pop is based soley on the cache key ordering).

Returns:Two-element tuple of deleted cache (key, value).
Return type:tuple
set(key, value, ttl=None)

Set cache key/value and replace any previously set cache key. If the cache key previous existed, setting it will move it to the end of the cache stack which means it would be evicted last.

Note

Cache key must be hashable.

Parameters:
  • key (mixed) – Cache key to set.
  • value (mixed) – Cache value.
  • ttl (int, optional) – TTL value. Defaults to None which uses ttl.
set_many(items, ttl=None)

Set multiple cache keys at once.

Parameters:items (dict) – Mapping of cache key/values to set.
size()

Return number of cache entries.

values()

Return dict_values view of all cache values.

Note

Cache is copied from the underlying cache storage before returning.

Returns:dict_values