Skip to main content

This simple generic cache implementation is Copyright(c) Pilot Systems, 2006-2008 and available under the terms of the GNU GPL v2.

Version information

Version 1.0.2, requires Python 2.4 or higher.

Features

  • fast in memory caching of any object

  • transparent dictionnary-like usage

  • possibility to limit the cache size in number of objects

  • possibility to expire automatically objects after a given time of no update

  • thread-safe

Basic usage

Creating a cache:

>>> from GenericCache.GenericCache import GenericCache
>>> c = GenericCache()

Storing values:

>>> c["key"] = "value"
   or
>>> c.insert("key", "value")

Retrieving values:

>>> c["key"]
   or
>>> c.fetch("key")

None is returned if no value is found in the cache. Fetch can take an optional value to fail with KeyError if no value is found.

Warnings

  • in current implementation keys are converted into strings

  • object expiry for timeout is lazy, which means that objects stay in the cache until someone try to access them, len() will still see them; there is a collect() method to collect all timeouted objects, but it may be slow.

Decorator usage

The decorator, for Python 2.4+, can be used to decorate any function or method. Here is an example:

>>> from GenericCache.GenericCache import GenericCache
>>> from GenericCache.decorators import cached
>>> cache = GenericCache()
>>> @cached(cache)
... def add(x, y):
...     print x, "+", y
...     return x + y
...
>>> add(2,3)
2 + 3
5
>>> add(2,3)
5
>>> add(2,4)
2 + 4
6
>>> add(2,4)
6
>>> add(2,3)
5
>>>

The decorator takes an optional parameter, a marshaller that filter the paramters of the function and converts it into a caching key (some parameters may be discarded, for example).

Release files for GenericCache 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for GenericCache 1.0.2
File Size Uploaded
GenericCache-1.0.2.tar.gz 12.2 kB Details

Release files / GenericCache-1.0.2.tar.gz

Download URL GenericCache-1.0.2.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
40bab49a96bba1b020d0b79e649c932d9924c0afe9c3426a5ae9a5f5bfa23878
BLAKE2b-256 checksum
How to use checksums
e3491eba17500ceaa7a9cfea7986edbc5d08aec0b48c7ed7a6bacd584615e68d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file

1.0.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page