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.2.tar.gz (15.5 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.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pickle_function_cache-0.1.2.tar.gz
  • Upload date:
  • Size: 15.5 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.2.tar.gz
Algorithm Hash digest
SHA256 549be8218d7a9f3ed75b03109091331fda0867335d550e6216346507b52e7465
MD5 fcbbd8b8949b9476ef954d15b75035dd
BLAKE2b-256 4688b0ca6583bee452fdd4a8eab5332fd70728c8652429f19b9772b033db32c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pickle_function_cache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6bac6b359243afec8143d634993399f2aa0d9f928a97fc64a1df5f47549b2ddb
MD5 a563e6558a9babfc88952c5fa2ebc328
BLAKE2b-256 187d50f2e1b9c9ea06e585430bab97d76eeab2cdcff5fa22794a3242a96a8128

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