class BaseCache(object):
Known subclasses: pyzim.cache.HybridCache, pyzim.cache.LastAccessCache, pyzim.cache.NoOpCache, pyzim.cache.TopAccessCache
Constructor: BaseCache(on_leave)
Base class for caches.
When using any of the subclasses of this cache, please use something akin to the following pattern:
key = ... # some unique hashable key identifying the element
if cache.has(key): # cache hit
element = cache.get(key)
else:
element = ... # get element outside of cache
cache.push(key) # always push element unto the cache
This class does not actually implement anything and serves as a superclass that describes the methods.
| Method | __init__ |
The default constructor. |
| Method | clear |
Empty this cache. |
| Method | get |
Return the element that has been cached for this key. |
| Method | has |
Return True if an element is cached for the specific key. |
| Method | push |
Push an element for this key into this cache. |
| Method | remove |
If an element is cached for key, remove it from the cache. |
| Instance Variable | on |
a callable expecting two arguments (key, value) that will be called when an element leaves the cache |
The default constructor.
| Parameters | |
onNone | a callable expecting two arguments (key, value) that will be called when an element leaves the cache |
pyzim.cache.HybridCache, pyzim.cache.LastAccessCache, pyzim.cache.NoOpCache, pyzim.cache.TopAccessCacheEmpty this cache.
| Parameters | |
callbool | if nonzero (default), call BaseCache.on_leave for each element cleared |
pyzim.cache.HybridCache, pyzim.cache.LastAccessCache, pyzim.cache.NoOpCache, pyzim.cache.TopAccessCacheReturn the element that has been cached for this key.
| Parameters | |
| key:hashable | key for the element that should be returned |
| Returns | |
same as passed to BaseCache.push | the element |
| Raises | |
KeyError | if no element was cached for this key |
pyzim.cache.HybridCache, pyzim.cache.LastAccessCache, pyzim.cache.NoOpCache, pyzim.cache.TopAccessCacheReturn True if an element is cached for the specific key.
| Parameters | |
| key:hashable | key of element that should be checked |
| Returns | |
bool | True if an element has been cached for this key |
pyzim.cache.HybridCache, pyzim.cache.LastAccessCache, pyzim.cache.NoOpCache, pyzim.cache.TopAccessCachePush an element for this key into this cache.
It is up for the cache to decide whether the element will actually be cached or not. If allow_replacement=False is set, no other element should be kicked from the cache.
Re-pushing an already cached element will not update the cached element.
| Parameters | |
| key:hashable | key which will be used to identify the element |
| element:any | element to cache |
allowbool | if this value is false, do not cache if this would kick another element from the cache |
| Returns | |
bool | True if the element is now cached, False otherwise |
pyzim.cache.HybridCache, pyzim.cache.LastAccessCache, pyzim.cache.NoOpCache, pyzim.cache.TopAccessCacheIf an element is cached for key, remove it from the cache.
| Parameters | |
| key:hashable | key of element to remove |
callbool | if nonzero (default), call BaseCache.on_leave on the removed element |