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.7.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.7-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pickle_function_cache-0.1.7.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.7.tar.gz
Algorithm Hash digest
SHA256 3e3fb07c59b2b4a41313f4d13e2b762e6a70b8d1bb001086bc39cfc4cb945819
MD5 d479c67a800656544782e8463b72b802
BLAKE2b-256 f6d85f65455991de0daaebd68b2cf7d77a06a049a926518878849d01cac1835c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pickle_function_cache-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 67ddb29186dc3043ec2fa69b65cd48781e9b17cf8ceb85085477e60a7baa0dd0
MD5 1d8cccf2d57b5f82cf5784457dbf6dd4
BLAKE2b-256 eb2b282809375de70cc4e21bbdbe957496f2751ceb772e96f3d8d6036f1b3b4f

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