horton.cache – Avoid recomputation of earlier results and reallocation of existing arrays¶
In principle, the
JustOnceClass and the
Cache can be used
independently, but in some cases it makes a lot of sense to combine them.
See for example the density partitioning code in
Base class for classes with methods that should never be executed twice.
In typically applications, these methods get called many times, but only during the first call, an actual computation is carried out. This way, the caller can safely call a method, just to make sure that a required result is computed.
All methods in the subclasses that should have this feature, must be given the
class Example(JustOnceClass): @just_once def do_something(): self.foo = self.bar
When all results are outdated, one can call the
clearmethod to forget which methods were called already.
Object that stores previously computed results.
The cache behaves like a dictionary with some extra features that can be used to avoid recomputation or reallocation.
Clear all items in the cache
- When set to True, the items are really removed from memory.
- Limit the items cleared to those who have at least one tag that matches one of the given tags. When this argument is used and it contains at least one tag, items with no tags are not cleared.
Clear a selected item from the cache
- When set to True, the item is really removed from memory.
Store an object in the cache.
- key1 [, key2, ...]
- The positional arguments (except for the last) are used as a key for the object.
- The object to be stored.
- When set to True, the cache will take care of denouncing the memory usage due to this array.
- Tags to be associated with the object
Iterate over all valid items in the cache.
Iterate over the keys of all valid items in the cache.
Iterate over the values of all valid items in the cache.
Get a value from the cache
- key0 [key1 ...]
- All positional arguments are used as keys to identify the cached value.
- Parameters used to allocate a cached value if it is not present yet. This argument can take two forms. When integer or a tuple of integers is given, an array is allocated. Alternatively, a tuple may be given whose first element is a constructor, and further elements are arguments for that constructor.
- A default value that is returned when the key does not exist in the cache. This default value is not stored in the cache.
- When alloc is used and a new object is thereby created or reused, it will get these tags. This argument is only allowed if the alloc argument is present. In case no new object is allocated, the given tags must match those already present.
The optional argument alloc and default are both meant to handle situations when the key has not associated value. Hence they can not be both present.