ftrack_api.cache
Caching framework.
Defines a standardised Cache
interface for storing data against
specific keys. Key generation is also standardised using a KeyMaker
interface.
Combining a Cache and KeyMaker allows for memoisation of function calls with
respect to the arguments used by using a Memoiser
.
As a convenience a simple memoise()
decorator is included for quick
memoisation of function using a global cache and standard key maker.
- class ftrack_api.cache.Cache[source]
Cache interface.
Derive from this to define concrete cache implementations. A cache is centered around the concept of key:value pairings where the key is unique across the cache.
- keys()[source]
Return list of keys at this current time.
Warning
Actual keys may differ from those returned due to timing of access.
- clear(pattern=None)[source]
Remove all keys matching pattern.
pattern should be a regular expression string.
If pattern is None then all keys will be removed.
- __init__()
- class ftrack_api.cache.ProxyCache(proxied)[source]
Proxy another cache.
- keys()[source]
Return list of keys at this current time.
Warning
Actual keys may differ from those returned due to timing of access.
- clear(pattern=None)
Remove all keys matching pattern.
pattern should be a regular expression string.
If pattern is None then all keys will be removed.
- values()
Return values for current keys.
- class ftrack_api.cache.LayeredCache(caches)[source]
Layered cache.
- get(key)[source]
Return value for key.
Raise
KeyError
if key not found.Attempt to retrieve from cache layers in turn, starting with shallowest. If value retrieved, then also set the value in each higher level cache up from where retrieved.
- keys()[source]
Return list of keys at this current time.
Warning
Actual keys may differ from those returned due to timing of access.
- clear(pattern=None)
Remove all keys matching pattern.
pattern should be a regular expression string.
If pattern is None then all keys will be removed.
- values()
Return values for current keys.
- class ftrack_api.cache.MemoryCache[source]
Memory based cache.
- keys()[source]
Return list of keys at this current time.
Warning
Actual keys may differ from those returned due to timing of access.
- clear(pattern=None)
Remove all keys matching pattern.
pattern should be a regular expression string.
If pattern is None then all keys will be removed.
- values()
Return values for current keys.
- class ftrack_api.cache.FileCache(path)[source]
File based cache that uses
anydbm
module.Note
No locking of the underlying file is performed.
- keys()[source]
Return list of keys at this current time.
Warning
Actual keys may differ from those returned due to timing of access.
- clear(pattern=None)
Remove all keys matching pattern.
pattern should be a regular expression string.
If pattern is None then all keys will be removed.
- values()
Return values for current keys.
- class ftrack_api.cache.SerialisedCache(proxied, encode=None, decode=None)[source]
Proxied cache that stores values as serialised data.
- __init__(proxied, encode=None, decode=None)[source]
Initialise cache with encode and decode callables.
proxied is the underlying cache to use for storage.
- clear(pattern=None)
Remove all keys matching pattern.
pattern should be a regular expression string.
If pattern is None then all keys will be removed.
- keys()
Return list of keys at this current time.
Warning
Actual keys may differ from those returned due to timing of access.
- values()
Return values for current keys.
- class ftrack_api.cache.StringKeyMaker[source]
Generate string key.
- __init__()
Initialise key maker.
- key(*items)
Return key for items.
- class ftrack_api.cache.ObjectKeyMaker[source]
Generate unique keys for objects.
- key(*items)
Return key for items.
- class ftrack_api.cache.Memoiser(cache=None, key_maker=None, return_copies=True)[source]
Memoise function calls using a
KeyMaker
andCache
.Example:
>>> memoiser = Memoiser(MemoryCache(), ObjectKeyMaker()) >>> def add(x, y): ... "Return sum of *x* and *y*." ... print 'Called' ... return x + y ... >>> memoiser.call(add, (1, 2), {}) Called >>> memoiser.call(add, (1, 2), {}) >>> memoiser.call(add, (1, 3), {}) Called
- __init__(cache=None, key_maker=None, return_copies=True)[source]
Initialise with cache and key_maker to use.
If cache is not specified a default
MemoryCache
will be used. Similarly, if key_maker is not specified a defaultObjectKeyMaker
will be used.If return_copies is True then all results returned from the cache will be deep copies to avoid indirect mutation of cached values.
- ftrack_api.cache.memoise_decorator(memoiser)[source]
Decorator to memoise function calls using memoiser.
- ftrack_api.cache.memoiser = <ftrack_api.cache.Memoiser object>
Default memoiser.
- ftrack_api.cache.memoise(function)
Default memoise decorator using standard cache and key maker.