PyXWF.Cache – Caching baseclasses

Caching framework for PyXWF. This is probably not as mature as we may need it in the future, but it provides basic means for caching resources used by the PyXWF instance.

It may, in the future, also be used to cache complete rendered pages.

class PyXWF.Cache.Cachable[source]

Represents an object which can reside in a cache. This class defines the interface neccessary for a cache entry to work properly. The following attributes are used (and reserved by) the caching framework on Cachables:

  • _cache_lastaccess – timestamp of last access of the object via the cache. This is used as a metric of when an entry can be uncached if limits are reached.
  • _cache_master – The Cache instance holding the object.
propose_uncache()[source]

Set the timestamp of last use in the past so that uncaching of this object in case of reached limits is more likely than for other objects.

touch()[source]

Set the internal timestamp of last use to the current timestamp. This will also inform the master (if known) of the changed value for uncache metrics.

uncache()[source]

Remove the object from the cache it is associated to.

class PyXWF.Cache.Cache(site, limit=0)[source]

Master object representing a full application cache. Objects are not directly stored in the Cache instance but in sub-caches. The Cache object provides dictionary-like access to sub caches. If no cache is associated with a certain key, a new raw SubCache is created for that key.

Specialized sub caches can be created using specialized_cache().

Limit None[source]

How many cache entries are kept at max. This does not differentiate between different sub-caches and is a global hard-limit. If this limit is exceeded, old (i.e. not used for some time) entries are purged.

Setting this limit to 0 will disable limiting.

enforce_limit()[source]

Remove those entries with the oldest lastAccess from the cache.

remove(cachable)[source]

Remove one entry from the cache. You can either use this or CacheEntry.delete(), which does the same thing.

specialized_cache(key, cls, *args, **kwargs)[source]

Create a specialized subcache using the given class cls at the given key. Further arguments and keyword arguments are passed to the constructor of cls.

Return the new cls instance.

class PyXWF.Cache.FileSourcedCache(master, rootpath)[source]

This is an abstract baseclass for caches which are backed by files on the file system. The file names are used as keys relative to a given root directory rootpath.

A deriving class has to implement the _load method which is called if a file accessed through this cache is not available in the cache.

get_last_modified(key)[source]

In contrast to the implementation given in SubCache, this implementation uses the timestamp of last modification of the file referenced by key. This implies that a resource is not neccessarily loaded (or even loadable!) even if a LastModified can be retrieved successfully.

class PyXWF.Cache.SubCache(cache)[source]

The big master cache (Cache instance) is subdivided into smaller parts, SubCache instances, which can be thought of namespaces inside the cache.

They’re used to separate different kinds of cachable objects, which can be handled by different SubCache-derived classes. See FileSourcedCache as an example for optimizations possible using this.

SubCaches provide dictionary-like access to cachables, associating keys to the cachables. An object can be added to the cache by simply assigning a key to it. Objects can also be uncached by using the del operator. The in operator and the len function work properly.

get(key, default=None)[source]

Try to get an object from the cache and return default (defaults to None) if no object is associated with key.

get_last_modified(key)[source]

Return the datetime representing the last modification of the cached content. The default implementation requests the cached element from the cache and queries the LastModified property.

Derived classes may (and should!) provide mechanisms which can query the LastModified timestamp without completely loading an object.

remove(cachable)[source]

Remove a cachable from the Cache.

update(key)[source]

Call update() on the cache entry referenced by key, but only if key references a valid entry. Otherwise, nothing happens. This is used to ensure that cached entries are reloaded if they wouldn’t be reloaded anyways on the next access.

Previous topic

Caching and resource management

Next topic

PyXWF.Resource – Baseclass for a reloadable resource

This Page