Class AsyncLRUCache
source code
object --+
|
AsyncLRUCache
A least-recently-used cache, with a fixed maximum size. This cache is
designed to control memory usage by minimizing duplication of objects,
while avoiding unnecessary re-fetching of the same rows from the
database.
Asynchronous locking is used to ensure that in the common case of
multiple concurrent requests for the same key, only one fetch is
performed.
All values are also stored in a weak valued dictionary, even after
they have expired from the cache. This allows values that are used
elsewhere in Buildbot to "stick" in the cache in case they are
needed by another component. Weak references cannot be used for some
types, so these types are not compatible with this class. Note that
dictionaries can be weakly referenced if they are an instance of a
subclass of dict
.
If the result of the miss_fn
is None
, then
the value is not cached; this is intended to avoid caching negative
results.
This is based on Raymond Hettinger's implementation in http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/
licensed under the PSF license, which is GPL-compatiblie.
|
|
|
get(self,
key,
**miss_fn_kwargs)
Fetch a value from the cache by key, invoking
self.miss_fn(key) if the key is not in the cache. |
source code
|
|
|
put(self,
key,
value)
Update the cache with the given key and value, if the key is already
in the cache. |
source code
|
|
|
|
|
inv(self)
Check invariants and log if they are not met; used for debugging |
source code
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
|
sentinel = object()
|
|
QUEUE_SIZE_FACTOR = 10
|
|
hits
cache hits so far
|
|
max_size
maximum allowed size of the cache
|
|
misses
cache misses leading to re-fetches, so far
|
|
refhits
cache misses found in the weak ref dictionary, so far
|
|
cache
|
|
concurrent
|
|
max_queue
|
|
miss_fn
|
|
queue
|
|
refcount
|
|
weakrefs
|
Inherited from object :
__class__
|
__init__(self,
miss_fn,
max_size=50)
(Constructor)
| source code
|
Constructor.
- Parameters:
miss_fn - function to call, with key as parameter, for cache misses. This
function must return a deferred.
max_size - maximum number of objects in the cache
- Overrides:
object.__init__
|
Fetch a value from the cache by key, invoking
self.miss_fn(key) if the key is not in the cache.
Any additional keyword arguments are passed to the
miss_fn as keyword arguments; these can supply additional
information relating to the key. It is up to the caller to ensure that
this information is functionally identical for each key value: if the key
is already in the cache, the miss_fn will not be invoked,
even if the keyword arguments differ.
- Parameters:
key - cache key
**miss_fn_kwargs - keyword arguments to the miss_fn
- Returns:
- value via Deferred
|
Update the cache with the given key and value, if the key is already
in the cache. This is intended to be used when updated values are
available for an existing cached object, and does not record a reference
to the key.
- Parameters:
key - key to update
value - new value
- Returns:
- nothing
|