Skip to main content

filecache

filecache is a decorator which saves the return value of functions even after the interpreter dies. For example this is useful on functions that download and parse webpages. All you need to do is specify how long the return values should be cached (use seconds, like time.sleep).

USAGE:

from filecache import filecache

@filecache(24 * 60 * 60)
def time_consuming_function(args):
    # etc

@filecache(filecache.YEAR)
def another_function(args):
    # etc

NOTE: All arguments of the decorated function and the return value need to be picklable for this to work.

NOTE: The cache isn't automatically cleaned, it is only overwritten. If your function can receive many different arguments that rarely repeat, your cache may forever grow. One day I might add a feature that once in every 100 calls scans the db for outdated stuff and erases.

NOTE: This is less useful on methods of a class because the instance (self) is cached, and if the instance isn't the same, the cache isn't used. This makes sense because class methods are affected by changes in whatever is attached to self.

Tested on python 2.7 and 3.1

License: BSD, do what you wish with this. Could be awesome to hear if you found it useful and/or you have suggestions. ubershmekel at gmail

A trick to invalidate a single value:

@filecache.filecache
def somefunc(x, y, z):
    return x * y * z

del somefunc._db[filecache._args_key(somefunc, (1,2,3), {})]
# or just iterate of somefunc._db (it's a shelve, like a dict) to find the right key.

Release files for filecache 0.81

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

Source distribution (sdist)

Source distribution for filecache 0.81
File Size Uploaded
filecache-0.81.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for filecache 0.81
File Interpreter ABI Platform
filecache-0.81-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / filecache-0.81.tar.gz

Download URL filecache-0.81.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
be071ad64937b51f38b03ecd82b9b68c08d0f570cdddb30aa8f90150fe54b30a
BLAKE2b-256 checksum
How to use checksums
b3f5647f13b1cae32f8d3b84866f6bac688b7923c5d7643b994e5e89865c9a2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release files / filecache-0.81-py3-none-any.whl

Download URL filecache-0.81-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91ce1a42b532d0e9ad75364c13159bafc3015973d4a5a0dbf37e4b4feb194055
BLAKE2b-256 checksum
How to use checksums
eb79f96a2addff21798ea11aa51ae15052514e9ac0ab4ab9470ddd1a0da6fd3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.81 This release

2 release files

0.77

1 release file

0.75

1 release file

0.71

1 release file

0.69

1 release file

0.67

1 release file

0.66

1 release file

0.65

1 release file

0.64

2 release files

0.63

1 release file

0.62

1 release file

0.61

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