Skip to main content

Caching of the function results in a 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.4.tar.gz (16.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.4-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pickle_function_cache-0.1.4.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.1

File hashes

Hashes for pickle_function_cache-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2c04ab105b9f3a50415cb333c499d62b245f71421b726e977adade7e04dd5e9d
MD5 44cb554babab41939ce54627bb1e6b84
BLAKE2b-256 badc107c6a825a88c5b704729fc94b0ed2225b9d390b79f088e6306a43eb6ab4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pickle_function_cache-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 723d6eefc8b725d0cafa5fb56ae9626409cd080b9c8be5e1286dc4a962f89b7f
MD5 f3794e967fc37fd449778eb6f3efed1c
BLAKE2b-256 f612e0174820688a0a47a175fae6c5fe3fbd86528910d7e146c1da862d157dc0

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