Skip to main content

Caching of function results in a pickle file.

Project description

Pickle function cache

Allows for caching of the results of function calls in a file using pickle (builtin shelve did not work for some reason and marshal was not considered; json was not able to serialize all the things I needed).

  • the file read happens only once per process per filename (all the function caches in the same cache file are loaded once)
  • if the result is not found in the cache, the result is computed using the original function and is written into the cache file and to the runtime cache.
  • if the result for the same set of arguments is requested and found the original function is not called but the cached result is returned.

Usage

from pickle_function_cache import cache_responses


@cache_responses("cache.dat")
def f1(param1, param2):
    ...  # take a lot of time


@cache_responses("cache.dat")
def f2(param1, param2, param3):
    ...  # take a lot of time

# use function normally ...
  • when first called, f1(1,2) computes the result using the original, f1
  • afterwards f1(1,2) returns the cached result, omitting the function call
  • if you call f2(1,2,3), it will be no trouble because function caches are stored separately per function name in the same file (note: cached function names should be unique)

Configuration

You can set the env var OMIT_PICKLE_FUNCTION_CACHE to either 1 or true to disable the cache at compile time without changing the code.

Performance

The cache was tested on a small set of quick functions and a small test list of examples and did not introduce a significant overhead. Probably, if the chosen functions for cache were more computationally intensive, we would see a speed-up because of the cache.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pickle_function_cache-0.1.6.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pickle_function_cache-0.1.6-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file pickle_function_cache-0.1.6.tar.gz.

File metadata

  • Download URL: pickle_function_cache-0.1.6.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pickle_function_cache-0.1.6.tar.gz
Algorithm Hash digest
SHA256 4ec5dabad348fb27b1f9b821ef18a4f775d6f93d2a609bbf354c84115f13a0eb
MD5 a37ba8b1db3755b0db467e208709c8fa
BLAKE2b-256 b0d66d41d82ca98e435b3827cb92f19fc5bb5aa2032b984363fde15fe2ca206c

See more details on using hashes here.

File details

Details for the file pickle_function_cache-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pickle_function_cache-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b48155c70a286cacf7ca540fb33a74e9fcbd52530b5d4dc64dcd5c92f781f721
MD5 7e2fb89f6d071f344b54a490aa1b92d4
BLAKE2b-256 ce18d40a8eebda740ef292fabfbd6299e6cc11340c0d679bff0ba032b44e9be5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page