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.

abstract get(key)[source]

Return value for key.

Raise KeyError if key not found.

abstract set(key, value)[source]

Set value for key.

abstract remove(key)[source]

Remove key and return stored value.

Raise KeyError if key not found.

keys()[source]

Return list of keys at this current time.

Warning

Actual keys may differ from those returned due to timing of access.

values()[source]

Return values for current keys.

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.

__init__(proxied)[source]

Initialise cache with proxied cache instance.

get(key)[source]

Return value for key.

Raise KeyError if key not found.

set(key, value)[source]

Set value for key.

remove(key)[source]

Remove key and return stored value.

Raise KeyError if key not found.

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.

__init__(caches)[source]

Initialise cache with caches.

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.

set(key, value)[source]

Set value for key.

remove(key)[source]

Remove key.

Raise KeyError if key not found in any layer.

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.

__init__()[source]

Initialise cache.

get(key)[source]

Return value for key.

Raise KeyError if key not found.

set(key, value)[source]

Set value for key.

remove(key)[source]

Remove key.

Raise KeyError if key not found.

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.

__init__(path)[source]

Initialise cache at path.

get(key)[source]

Return value for key.

Raise KeyError if key not found.

set(key, value)[source]

Set value for key.

remove(key)[source]

Remove key.

Raise KeyError if key not found.

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.

get(key)[source]

Return value for key.

Raise KeyError if key not found.

set(key, value)[source]

Set value for key.

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.

remove(key)

Remove key and return stored value.

Raise KeyError if key not found.

values()

Return values for current keys.

class ftrack_api.cache.KeyMaker[source]

Generate unique keys.

__init__()[source]

Initialise key maker.

key(*items)[source]

Return key for items.

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.

__init__()[source]

Initialise key maker.

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 and Cache.

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 default ObjectKeyMaker 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.

call(function, args=None, kw=None)[source]

Call function with args and kw and return result.

If function was previously called with exactly the same arguments then return cached result if available.

Store result for call in cache.

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.