CacheFactory

CacheFactory caches object creation. Each object should be referenced by a single hashable ID (note tuples of hashable values are also hashable).


Methods

f __format__(...) ...

default object formatter

f __subclasshook__(...) ...

Abstract classes can override this to customize issubclass().

This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).

f __sizeof__(...) ...

__sizeof__() -> int size of object in memory, in bytes

f __init__(self, cullFrequency=100, cullFraction=2, cache=True) ...

Every cullFrequency times that an item is retrieved from this cache, the cull method is called.

The cull method then expires an arbitrary fraction of the cached objects. The idea is at no time will the cache be entirely emptied, placing a potentially high load at that moment, but everything object will have its time to go eventually. The fraction is given as an integer, and one in that many objects are expired (i.e., the default is 1/2 of objects are expired).

By setting cache to False, items won't be cached.

However, in all cases a weak reference is kept to created objects, and if the object hasn't been garbage collected it will be returned.

f tryGet(self, id) ...

This returns None, or the object in cache.

f get(self, id) ...

This method can cause deadlocks! tryGet is safer

This returns the object found in cache, or None. If None, then the cache will remain locked! This is so that the calling function can create the object in a threadsafe manner before releasing the lock. You should use this like (note that cache is actually a CacheSet object in this example):

obj = cache.get(some_id, my_class)
if obj is None:
    try:
        obj = create_object(some_id)
        cache.put(some_id, my_class, obj)
    finally:
        cache.finishPut(cls)

This method checks both the main cache (which retains references) and the 'expired' cache, which retains only weak references.

f put(self, id, obj) ...

Puts an object into the cache. Should only be called after .get(), so that duplicate objects don't end up in the cache.

f finishPut(self) ...

Releases the lock that is retained when .get() is called and returns None.

f created(self, id, obj) ...

Inserts and object into the cache. Should be used when no one else knows about the object yet, so there cannot be any object already in the cache. After a database INSERT is an example of this situation.

f cull(self) ...

Runs through the cache and expires objects

E.g., if cullFraction is 3, then every third object is moved to the 'expired' (aka weakref) cache.

f clear(self) ...

Removes everything from the cache. Warning! This can cause duplicate objects in memory.

f expire(self, id) ...

Expires a single object. Typically called after a delete. Doesn't even keep a weakref. (@@: bad name?)

f expireAll(self) ...

Expires all objects, moving them all into the expired/weakref cache.

f allIDs(self) ...

Returns the IDs of all objects in the cache.

f getAll(self) ...

Return all the objects in the cache.

See the source for more information.