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=[2])
def add_ignore(x, y, z):
    return x + y

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

# this will be retrieved from cache since z is ignored
print(add_ignore(1, 2, -5))

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.0.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.

def_cache-1.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: def_cache-1.1.0.tar.gz
  • Upload date:
  • Size: 4.1 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.0.tar.gz
Algorithm Hash digest
SHA256 02ec9efa074ef47c8615d21fecd86eb0743f998ecd805e506b22f56bce130659
MD5 02f54825715a9c7a0824235ff8d55553
BLAKE2b-256 3dfbfe53bac15fc9c5e7394d05618d29423d685c47f670fba15cf601653b05e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: def_cache-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41655e26ecf65700e8c4388deb1c3e67f4d5bc6d15efeb5c4337888117308402
MD5 1ac792ebc1d364047a5509c8b640bc38
BLAKE2b-256 be1046268ab0e78a0124894e9f3e170613bfd9efcfe96543c999410fc0cd2b5e

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