Skip to main content

def-cache is a python package used to cache results from python methods

Project description

def-cache

PyPI Version Python Versions PyPI Downloads

def-cache is a python package that can be used as a method decorator to case their results.

Even though it can be used for any python method, it's aim is to be used in computational heavy methods, that do not need to be executed constantly and can be cached (eg: model training, heavy calculation tasks, etc)

Currently, the backend supported is fs (file-system) and the results of the cached method are stored in files

Installation

As def-cache is a python package it can be installed directly using pip:

python -m pip install def-cache

Alternatively one can use the source code directly.

Usage

Upon installation one can directly use the decorator on the methods that need to be cached.

A base usage example can be found below:

from def_cache import decorator

"""
The decorator below will cache the results of method: add for 60s in a file stored in the tmp relative path
"""
@decorator.cache(ttl=60, backend='fs', storage='tmp')
def add(x, y):
    return x + y

# this will not be called from cache
print(add(1, 2))

# this will be retrieved from cache
print(add(1, 2))


"""
The decorator below will cache the results of method: add for 60s in a file stored in the tmp relative path 
ignoring the 3rd parameter when attempting to cache
"""
@decorator.cache(ttl=60, backend='fs', storage='tmp', ignore_by_index=[2])
def add_ignore_by_index(x, y, z):
    return x + y

# this will not be called from cache
print(add_ignore_by_index(1, 2, -1))

# this will be retrieved from cache since parameter#3 is ignored
print(add_ignore_by_index(1, 2, -5))

"""
The decorator below will cache the results of method: add for 60s in a file stored in the tmp relative path 
ignoring the parameter name `z` when attempting to cache
"""
@decorator.cache(ttl=60, backend='fs', storage='tmp', ignore_by_name=['z'])
def add_ignore_by_name(x, y, z):
    return x + y

# this will not be called from cache
print(add_ignore_by_name(x=1, y=2, z=10))

# this will be retrieved from cache since z is ignored
print(add_ignore_by_name(x=1, y=2, z=15))

Example are also present in the examples directory.

Future Extensions

In the future we plan on adding storage engine support for the decorator, but of course would welcome any suggestions.

Download files

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

Source Distribution

def_cache-1.1.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

def_cache-1.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file def_cache-1.1.2.tar.gz.

File metadata

  • Download URL: def_cache-1.1.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for def_cache-1.1.2.tar.gz
Algorithm Hash digest
SHA256 c5a1424d2bc8826cc5c0893fda14e05b28f6a103cfe2417f1aed91b022f825d1
MD5 720438d32083a43ac1e658f631ca281c
BLAKE2b-256 f522095bef20968a0e23c27af4f6091bc8eb44bba26859fbc2d43f314ce05142

See more details on using hashes here.

File details

Details for the file def_cache-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: def_cache-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for def_cache-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e39356bb7ebd082b0859e4c1dd3ec93fb8fe0a13bd9f43a8a54a3a19d787e31a
MD5 364d857eb86e102b8c0630d4c2b03f69
BLAKE2b-256 6cf384031dba8f8a116e0e680fd40e3cecc68391d0aedf9e43d28dd11918c875

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