A decorator for caching functions that provides persistence to JSON, pickle, or SQLite
Project description
persistent-cache-decorator
Table of Contents
Installation
pip install persistent-cache-decorator
Usage
>>> from typing import reveal_type
>>> from persistent_cache_decorator import persistent_cache
>>> # from persistent_cache_decorator import json_cache # Can also use one of this specific cache methods
>>> # from persistent_cache_decorator import pickle_cache # Can also use one of this specific cache methods
>>> # from persistent_cache_decorator import sqlite_cache # Can also use one of this specific cache methods
>>>
>>> import time
>>>
>>> @persistent_cache(minutes=4)
... def long_func(n: int) -> str:
... """Long Func Documentation"""
... # Long function
... time.sleep(n)
... return f"{n}"
...
>>> reveal_type(long_func)
Runtime type is '_persistent_cache'
<persistent_cache_decorator._persistent_cache object at 0x10468be50>
>>>
>>> # Call function(takes 5 seconds)
>>> long_func(5)
'5'
>>> # Call function again (takes 0 seconds)
>>> long_func(5)
'5'
>>>
>>> # Bypass caching(takes 5 seconds)
>>> long_func.no_cache_call(5)
'5'
>>>
>>> # Call function again (takes 0 seconds)
>>> long_func(5)
'5'
>>> # Clear cache for this function
>>> long_func.cache_clear()
>>>
>>> # Call function(takes 5 seconds)
>>> long_func(5)
Creating a custom cache backend
# Define a custom cache backend
class RedisCacheBackend:
def get_cached_results(self, *, func: Callable[..., _R], args: tuple[Any, ...], kwargs: dict[str, Any], lifespan: datetime.timedelta) -> _R: ...
def del_function_cache(self, *, func: Callable[..., Any]) -> None: ...
# Singleton Instance
REDIS_CACHE_BACKEND = RedisCacheBackend()
# Quick way of defining a decorator. You can use this if you want multiple decorators with different cache durations.
# It does have some typing hinting issues though :/
quick_redis_cache = cache_decorator_factory(backend=REDIS_CACHE_BACKEND)
@quick_redis_cache(days=1)
def foo(time: float) -> float:
from time import sleep
sleep(time)
return time
# This is the recommended way of defining a decorator. It has better typing hinting.
def redis_cache(
**duration: Unpack[_cache_duration]
) -> Callable[[Callable[_P, _R]], _persistent_cache[_P, _R, RedisCacheBackend]]:
duration = duration or {'days': 1} # You can set your own default cache duration.
mcache_duration = datetime.timedelta(**duration)
def inner(func: Callable[_P, _R]) -> _persistent_cache[_P, _R, RedisCacheBackend]:
return _persistent_cache(
func=func,
duration=mcache_duration,
backend=REDIS_CACHE_BACKEND,
)
return inner
@redis_cache(days=1, seconds=1)
def foo2(time: float) -> float:
from time import sleep
sleep(time)
return time
License
persistent-cache-decorator
is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for persistent_cache_decorator-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31f3c8f5f110c2cad275e2276f5b649dd721a3d75b71340cc3dae1c73d4a4f53 |
|
MD5 | cffc6d249623bb38e680a44f62435e51 |
|
BLAKE2b-256 | 0005123ba0b87d9125a9d9ca8da879b4d2e41fc7e37c51d45b4f485de65cae3b |
Close
Hashes for persistent_cache_decorator-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0862de206f58d17d024aa1cc8bc1ba0b16b821a4b2b328a76f8471522299e0b9 |
|
MD5 | f9a750ac652a1c831f20aa26fb72aa97 |
|
BLAKE2b-256 | 228e03ef6bd9a65e4de3f1057b76edc325567c03c5ba928af74b656655bbd0f3 |