Version

Quick search

Table Of Contents

Cache manager

The cache manager can be used to store python objects attached to a unique key. The cache can be controlled in two ways: with a object limit or a timeout.

For example, we can create a new cache with a limit of 10 objects and a timeout of 5 seconds:

# register a new Cache
Cache.register('mycache', limit=10, timeout=5)

# create an object + id
key = 'objectid'
instance = Label(text=text)
Cache.append('mycache', key, instance)

# retrieve the cached object
instance = Cache.get('mycache', key)

If the instance is NULL, the cache may have trashed it because you’ve not used the label for 5 seconds and you’ve reach the limit.

class kivy.cache.Cache[source]

Bases: builtins.object

See module documentation for more information.

static append(category, key, obj, timeout=None)[source]

Add a new object to the cache.

Parameters
category: str

Identifier of the category.

key: str

Unique identifier of the object to store.

obj: object

Object to store in cache.

timeout: double (optional)

Time after which to delete the object if it has not been used. If None, no timeout is applied.

Raises

ValueError: If None is used as key.

Changed in version 2.0.0: Raises ValueError if None is used as key.

static get(category, key, default=None)[source]

Get a object from the cache.

Parameters
category: str

Identifier of the category.

key: str

Unique identifier of the object in the store.

default: anything, defaults to None

Default value to be returned if the key is not found.

static get_lastaccess(category, key, default=None)[source]

Get the objects last access time in the cache.

Parameters
category: str

Identifier of the category.

key: str

Unique identifier of the object in the store.

default: anything, defaults to None

Default value to be returned if the key is not found.

static get_timestamp(category, key, default=None)[source]

Get the object timestamp in the cache.

Parameters
category: str

Identifier of the category.

key: str

Unique identifier of the object in the store.

default: anything, defaults to None

Default value to be returned if the key is not found.

static print_usage()[source]

Print the cache usage to the console.

static register(category, limit=None, timeout=None)[source]

Register a new category in the cache with the specified limit.

Parameters
category: str

Identifier of the category.

limit: int (optional)

Maximum number of objects allowed in the cache. If None, no limit is applied.

timeout: double (optional)

Time after which to delete the object if it has not been used. If None, no timeout is applied.

static remove(category, key=None)[source]

Purge the cache.

Parameters
category: str

Identifier of the category.

key: str (optional)

Unique identifier of the object in the store. If this argument is not supplied, the entire category will be purged.