Caches

Faste supports several cache algorithms, each with their own benefits. If you want more information on any of these, you can find them on Wikipedia. https://en.wikipedia.org/wiki/Cache_replacement_policies

All of these caches support every dictionary method. https://docs.python.org/3/library/stdtypes.html#dict

Random Replacement

class faste.caches.RRCache(max_size, populate=None)

Random Replacement cache. When the cache reaches max_size, keys will randomly be evicted to make room.

Parameters:
  • max_size (int) – Max size of the cache. Must be > 0
  • populate (dict) – Keyword argument with values to populate cache with, in no given order. Can’t be larger than max_size

First In First Out

class faste.caches.FIFOCache(max_size, populate=None)

First In First Out cache. When the max_size is reached, the cache evicts the first key set/accessed without any regard to when or how often it is accessed.

Parameters:
  • max_size (int) – Max size of the cache. Must be > 0
  • populate (dict) – Keyword argument with values to populate cache with, in no given order. Can’t be larger than max_size

Least Recently Used

class faste.caches.LRUCache(max_size, populate=None)

Least recently used cache implementation. When the max size is reached, the least recently used value is evicted from the cache.

Parameters:
  • max_size (int) – Max size of the cache. Must be > 0
  • populate (dict) – Keyword argument with values to populate cache with, in no given order. Can’t be larger than max_size

Most Recently Used

class faste.caches.MRUCache(max_size, populate=None)

Most Recently Used cache. When the max size is reached, the most recently used value is evicted from the cache. This is useful in applications where older keys are more likely to be accessed.

Parameters:
  • max_size (int) – Max size of the cache. Must be > 0
  • populate (dict) – Keyword argument with values to populate cache with, in no given order. Can’t be larger than max_size

Least Frequently Used

class faste.caches.LFUCache(max_size, populate=None)

Least Frequently Used cache. When max_size is reached, the least frequently accessed item is evicted from the cache.

Parameters:
  • max_size (int) – Maximum # of items that can exist in the cache
  • populate (dict) – Keyword dict of values to pre-populate the cache with, in no given order.
least_frequent()

Gets key, value pair for least frequent item in cache

Returns:tuple
reset_frequencies(frequency=0)

Resets all stored frequencies for the cache

Keyword Arguments:
 frequency (int) – Frequency to reset to, must be >= 0

Timeout based cache

class faste.caches.TimeoutCache(timeout, max_size=None, populate=None)

Cache where values timeout instead of being evicted. Once a value has existed in the cache for timeout seconds, it is evicted.

Parameters:
  • timeout (int) – Cache timeout in seconds. Must be > 0.
  • max_size (int) – Keyword to give a max amount of items. When this is reached, oldest item is evicted.
  • populate (dict) – Keyword dict to pre-populate cache with. Values will be evicted after the timeout, just like any others.

You can change the timeout at any time by changing timeout

oldest()

Gets key, value pair for oldest item in cache

Returns:tuple
time_left(key)

Gets the amount of time an item has left in the cache (in seconds), before it is evicted.

Parameters:key – Key to check time for.
Returns:int