Skip to main content

A library providing a decorator wrapper for adding the caching mechanism to functions.

Project description

Cache Return

This library provide a simple wrapper for custom functions to get the caching mechanism.

Use cases and benefits

With the caching mechanism activated, the result of a sub-function will be cached in the dedicated folder in the initial run. And the in the following runs, the cached result will loaded to bypass the actual internal process of a function.

This will be mainly useful for testing or debug tasks of larger projects with sophisticated sub-functions. As it will

  1. Avoid the actual interanl function run, thus save resources (e.g. queries/calls to APIs or databases) and improve efficiency.
  2. Achieve quicker testing processes and circles, as the reloading process is generally much faster than the function run.
  3. Provide the possibility for "offline" debugging and investigation for the real cases, as the cached return result can be manually accessed for investigation.

Examples

To add caching mechanism to a custom function, simply add the decorator wrapper to the function definition as below.

from cache_return import cache_return

@cache_return
def custom_function(arg_a, arg_b='default_value'):
    ... internal processes ...
    return results

Then custom function can be usage the same way as before. To activate its caching mechanism, simply provide an additional keyword argument caching=True.

# Before

return_result = custom_function(arg_a, arg_b='actual_value')

# After

return_result = custom_function(arg_a, arg_b='actual_value', caching=True)

To turn the caching mechanism off in the production code, you can either set the keyword argument as caching=False, or simple remove the caching=True keyword argument, as the default value caching=False will be used in this case.

The actual cached result will be saved in the auto-created folder ./_cache_ (sitting at the same directory as the top-level running script, not the script containing the custom function.), with the same name of the custom function as a pickle file

Then the cached result can be manully accessed in other places by the code below

import pickle

with open('./_cache_/custom_function.pkl', 'rb') as f:
    return_result = pickle.load(f)

Special note for environment with pandas version >= 2.0.0

The above custom investigation code has to be adjusted to the below code as

import pandas as pd

with open('./_cache_/custom_function.pkl', 'rb') as f:
    return_result = pd.compat.pickle_compat.load('./_cache_/custom_function.pkl') 

Or, you can downgrade pandas to the 1.x series by pip install pandas<2.0.0, see the stackoverflow topic here.

Additional arguments

In some cases, if you only want to overwrite/update the cached result in the new function run, you can achieve this by set the flushing keyword argument as flushing=True.

By default the cached result will be save under directory ./_cache_. An alternative directory custom_dir can be set by the argument cache_path as cache_path=custom_dir. Such a directory will be automatically created if it does not exist.

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

cache_return-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

cache_return-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cache_return-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for cache_return-0.1.2.tar.gz
Algorithm Hash digest
SHA256 99484947a43da9dd7ee5e78ddd8db072a684c99f17fb3438cac08d005e269c7e
MD5 b625bb19dec9da3a24878b2f9aba0c85
BLAKE2b-256 1b6b55112c0d6ee0689bb17552cb6089a86d2e0968b7b08eca18d3c0b2d47e00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cache_return-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for cache_return-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 649d8bc4b6a6b5a6247c29f131099882aeefa708a9b49d03a0c926ff9b9df748
MD5 92bf9433b244d11f1e3bebd6016b698a
BLAKE2b-256 29fbc1fced10a8d5fe07fcba3259369df5fb4a0bc9e5899d814ea7488c662031

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