def-cache is a python package used to cache results from python methods
Project description
def-cache
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file def_cache-1.1.1.tar.gz.
File metadata
- Download URL: def_cache-1.1.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e64e76460489f4482017b7df966902b3c73ea5579182fcea26d2ba31383d1809
|
|
| MD5 |
93779fed50951be5e234f901f9437a9f
|
|
| BLAKE2b-256 |
d0c8d0ef910b966f72eb801e1f54b00cff4e97fab2c85adf5bc03fe9ec1b4e28
|
File details
Details for the file def_cache-1.1.1-py3-none-any.whl.
File metadata
- Download URL: def_cache-1.1.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00ffbac61b8c298419260e890b01bc8501dd6264350404b75d3637bd2d0efd8a
|
|
| MD5 |
7d039d7ba4471f17d99b1134750b07a5
|
|
| BLAKE2b-256 |
76d1889afb1c7bfedddec183d757759adab0de74165b444ef5a398a5007ed395
|