xoa.misc.SmallCache

xoa.misc.SmallCache#

class xoa.misc.SmallCache(maxsize=4)[source]#

A small cache that forgets the least recently used items

Parameters:

maxsize (int) – Maximal number of items

Example

In [1]: cache = SmallCache(maxsize=2)

In [2]: cache.get_or_create("a", lambda: 1)
Out[2]: 1

In [3]: cache.get_or_create("a", lambda: 2)
Out[3]: 1

In [4]: len(cache)
Out[4]: 1

In [5]: cache.clear()

In [6]: len(cache)
Out[6]: 0
__init__(maxsize=4)[source]#

Methods

clear()

Remove all the items

get_or_create(key, factory)

Get the item of a key, or create and store it with the factory callable