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.decorators import persistent_cache
>>> # from persistent_cache.decorators import json_cache # Can also use one of this specific cache methods
>>> # from persistent_cache.decorators import pickle_cache # Can also use one of this specific cache methods
>>> # from persistent_cache.decorators 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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5496aba7a570f9381be86e5277941d4101f849f43fc941a19ec631e988031cfe |
|
MD5 | c64c0a9fd5f665680b156a8b4b3f84f1 |
|
BLAKE2b-256 | e81e4016672008f7baa76908bc49cc5a9349aa1492d340f199eea1b72b912172 |
Close
Hashes for persistent_cache_decorator-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a82188e4baff9953ddcf4ddf910a60c736979783b0cca35eb27ebf7916f8cf1 |
|
MD5 | f58399b592945723080a2eae81f268d2 |
|
BLAKE2b-256 | fb2c6b9966158d5df1fef652b50e314e2232104a8f0274509f38eaaec8bee71d |