Metadata-Version: 2.4
Name: def-cache
Version: 1.1.2
Summary: def-cache is a python package used to cache results from python methods
Author-Email: Mihalis Papakonstantinou <mihalispapak@gmail.com>
License-File: LICENSE
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python
Classifier: Topic :: Internet
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development
Classifier: Typing :: Typed
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Project-URL: Homepage, https://github.com/mihalispap/def-cache
Project-URL: Issues, https://github.com/mihalispap/def-cache/issues
Requires-Python: >=3.8
Description-Content-Type: text/markdown

## def-cache

<p align="center">
  <a href="https://pypi.org/project/def-cache"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/def-cache" /></a>
  <a href="https://pypi.org/project/def-cache"><img alt="Python Versions" src="https://img.shields.io/pypi/pyversions/def-cache" /></a>
  <a href="https://pepy.tech/projects/def-cache"><img src="https://static.pepy.tech/badge/def-cache" alt="PyPI Downloads"></a>
<br/>
</p>

`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:

```bash
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:

```python
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](/examples) directory.

### Future Extensions

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