bucketcache.buckets

class bucketcache.buckets.Bucket(path, backend=None, config=None, keymaker=None, lifetime=None, **kwargs)[source]

Dictionary-like object backed by a file cache.

Parameters:
  • backend (Backend) – Backend class. Default: PickleBackend
  • path – Directory for storing cached objects. Will be created if missing.
  • config (Config) – Config instance for backend.
  • keymaker (KeyMaker) – KeyMaker instance for object -> key serialization.
  • lifetime (timedelta) – Key lifetime.
  • kwargs – Keyword arguments to pass to datetime.timedelta as shortcut for lifetime.
setitem(key, value)[source]

Provide setitem method as alternative to bucket[key] = value

getitem(key)[source]

Provide getitem method as alternative to bucket[key].

prune_directory()[source]

Delete any objects that can be loaded and are expired according to the current lifetime setting.

A file will be deleted if the following conditions are met:

Returns:File size and number of files deleted.
Return type:PrunedFilesInfo

Note

For any buckets that share directories, prune_directory will affect files saved with both, if they use the same backend class.

This is not destructive, because only files that have expired according to the lifetime of the original bucket are deleted.

unload_key(key)[source]

Remove key from memory, leaving file in place.

__call__(*args, **kwargs)[source]

Use Bucket instance as a decorator.

@bucket
def fun(...):
    ...

Use method=True for instance methods:

@bucket(method=True)
def fun(self, ...):
    ...

Use nocache=’argname’ for argument that can skip cache.

@bucket(nocache='refresh')
def fun(refresh=False):
    ...

fun(refresh=True)  # Cache not used.

Use ignore=[‘argname1’, ‘argname2’, ...] to ignore arguments when making cache key.

@bucket(ignore=['log'])
def get(name, log):
    ...

get('spam')
get('spam', log=True)  # Cache used even though arguments differ.
class bucketcache.buckets.DeferredWriteBucket(path, backend=None, config=None, keymaker=None, lifetime=None, **kwargs)[source]

Alternative implementation of Bucket that defers writing to file until sync() is called.

unload_key(key)[source]

Remove key from memory, leaving file in place.

This forces sync().

sync()[source]

Commit deferred writes to file.

bucketcache.buckets.deferred_write(bucket)[source]

Context manager for deferring writes of a Bucket within a block.

Parameters:bucket (Bucket) – Bucket to defer writes for within context.
Returns:Bucket to use within context.
Return type:DeferredWriteBucket

When the context is closed, the stored objects are written to file. The in-memory cache of objects is used to update that of the original bucket.

bucket = Bucket(path)

with deferred_write(bucket) as deferred:
    deferred[key] = value
    ...