labm8.cache

Transient and persistent caching mechanisms.

class labm8.cache.Cache

Cache for storing (key,value) relational data.

A cache is a dictionary with a limited subset of a the functionality.

clear()

Remove all items from cache.

get(key, default=None)

Retrieve an item from cache.

Parameters:
  • key – Item key.
  • default (optional) – Default value if item not found.
items()

Returns a generator for iterating over (key, value) pairs.

class labm8.cache.FSCache(root, escape_key=<function hash_key>)

Persistent filesystem cache.

Each key uniquely identifies a file. Each value is a file path.

Adding a file to the cache moves it into the cahce directory.

Members:
path (str): Root cache. escape_key (fn): Function to convert keys to file names.
clear()

Empty the filesystem cache.

This deletes the entire cache directory.

get(key, default=None)

Fetch from cache.

Parameters:
  • key – Key.
  • default (optional) – Value returned if key not found.
Returns:

Path to cached file.

Return type:

str

keypath(key)

Get the filesystem path for a key.

Parameters:key – Key.
Returns:Absolute path.
Return type:str
ls(**kwargs)

List files in cache.

Parameters:**kwargs – Keyword options to pass to fs.ls().
Returns:List of files.
Return type:iterable
class labm8.cache.JsonCache(path, basecache=None)

A persistent, JSON-backed cache.

Requires that (key, value) pairs are JSON serialisable.

write()

Write contents of cache to disk.

class labm8.cache.TransientCache(basecache=None)

An in-memory only cache.

clear()
get(key, default=None)
items()
labm8.cache.escape_path(key)

Convert a key to a filename by escaping invalid characters.

labm8.cache.hash_key(key)

Convert a key to a filename by hashing its value.