Provides decorators to cache/update/delete results to/from Redis.
Created to be used in a project, this package is published to github
for ease of management and installation across different modules.
Features
For function decorated with @cache
, the result will be serialized and
stored in Redis. If the function is called again with the same arguments
before the expiration, the value will be retrieved from Redis and deserialized
and reconstruct to its original (or compatible) python dtype.
Supported caching data types includes:
- boolean:
bool
& numpy.bool_
-> deserialize to bool
- integer:
int
& numpy.int
& numpy.uint
-> deserialize to int
- float:
float
& numpy.float_
-> deserialize to float
- decimal:
decimal.Decimal
-> deserialize to decimal.Decimal
- string:
str
-> deserialize to str
- bytes:
bytes
-> deserialize to bytes
- date:
datetime.date
-> deserialize to datetime.date
- time:
datetime.time
-> deserialize to datetime.time
- datetime:
datetime.datetime
& pandas.Timestamp
-> deserialize to datetime.datetime
- datetime64*:
numpy.datetime64
& time.struct_time
-> deserialize to datetime.datetime
- timedelta:
datetime.timedelta
& pandas.Timedelta
-> deserialize to datetime.timedelta
- timedelta64:
numpy.timedelta64
-> deserialize to datetime.timedelta
- None:
None
& numpy.nan
-> deserialize to None
- list:
list
of above supported data types -> deserialize to list
- tuple:
tuple
of above supported data types -> deserialize to list
- set:
set
of above supported data types -> deserialize to list
- frozenset:
frozenset
of above supported data types -> deserialize to list
- dict:
dict
of above supported data types -> deserialize to dict
- numpy.record:
numpy.record
of above supported data types -> deserialize to list
- numpy.ndarray:
numpy.ndarray
of above supported data types -> deserialize to np.ndarray
- pandas.Series:
pandas.Series
of above supported data types -> deserialize to pandas.Series
- pandas.DataFrame:
pandas.DataFrame
of above supported data types -> deserialize to pandas.DataFrame
Installation
Install from PyPi
pip install redisdecor
Install from github
pip install git+https://github.com/AresJef/RedisDecor.git
Compatibility
Only support for python 3.10 and above.
Usage (Setup)
import redisdecor as rd
import datetime, numpy as np, pandas as pd
cl = rd.get_client(host="127.0.0.1", db=10)
def gen_data(rows: int, offset: int = 0) -> pd.DataFrame:
time.sleep(1)
tz = datetime.timezone(datetime.timedelta(hours=8), "CUS")
dt = datetime.datetime.now()
dt = datetime.datetime(2023, 1, 1, 1, 1, 1, 1)
val = {
"bool": True,
"np_bool": np.bool_(False),
"int": 1 + offset,
"int64": np.int64(5 + offset),
"unit": np.uint(5 + offset),
"unit64": np.uint64(5 + offset),
"float": 1.1 + offset,
"float64": np.float64(4.4 + offset),
"decimal": Decimal("3.3"),
"str": "STRING",
"bytes": b"BYTES",
"datetime": dt + datetime.timedelta(offset),
"datetime_tz": (dt + datetime.timedelta(offset)).replace(tzinfo=tz),
"time": (dt + datetime.timedelta(hours=offset)).time(),
"time_tz": (dt + datetime.timedelta(hours=offset)).time().replace(tzinfo=tz),
"timedelta": datetime.timedelta(1 + offset),
"None": None,
}
return pd.DataFrame([val for _ in range(rows)])
prefix = "test"
@rd.cache(cl, prefix, 60)
def gen_data_cache(rows: int) -> pd.DataFrame:
return gen_data(rows, 0)
@rd.update(cl, prefix)
def gen_data_update(rows: int) -> bool | None:
return gen_data(rows, 1)
@rd.delete(cl, prefix)
def gen_data_delete(rows: int) -> bool | None:
return gen_data(rows, 0)
Usage (Cache)
from timeit import timeit
cl.flushall()
print("No. cache - 100 row".ljust(20), timeit(lambda: gen_data_cache(100), number=1))
print("Hit cache - 100 row".ljust(20), timeit(lambda: gen_data_cache(100), number=1))
print("No. cache - 90 row".ljust(20), timeit(lambda: gen_data_cache(90), number=1))
print("Hit cache - 90 row".ljust(20), timeit(lambda: gen_data_cache(90), number=1))
print(gen_data_cache(100))
Usage (Update)
print("Update cache - 100 row".ljust(20), timeit(lambda: gen_data_update(100), number=1))
print("Update status:", gen_data_update(100))
print("Update miss - 80 row".ljust(20), timeit(lambda: gen_data_update(80), number=1))
print("Update status:", gen_data_update(80))
print(gen_data_cache(100))
Usage (Delete)
print("Delete cache - 100 row".ljust(20), timeit(lambda: gen_date_delete(100), number=1))
print("Delete status:", gen_date_delete(100))
print("Delete miss - 80 row".ljust(20), timeit(lambda: gen_date_delete(80), number=1))
print("Delete status:", gen_date_delete(80))
print("Check key:", cl.get("test:100::"))
Acknowledgements
redisdecor is based on several open-source repositories.