Skip to main content

Provides decorators to cache/update/delete results to/from Redis.

Project description

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
# Decorators in this package relies on `Redis`, which is a
# subclass of `redis.StrictRedis`. Besides the arguments
# `decode_responses` is fixed to False, this subclass works
# the same as `redis.StrictRedis`.
cl = rd.get_client(host="127.0.0.1", db=10)

# A shared 'expensive' function for all three decorators
def gen_data(rows: int, offset: int = 0) -> pd.DataFrame:
    # Add some delay to simulate expensiveness
    time.sleep(1)
    # Generate a pandas DataFrame
    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)])

# Shared prefix for all three decorators
prefix = "test"

# @cache decorator - returns function value
@rd.cache(cl, prefix, 60)
def gen_data_cache(rows: int) -> pd.DataFrame:
    return gen_data(rows, 0)

# @update decorator - return bool | None
@rd.update(cl, prefix)
def gen_data_update(rows: int) -> bool | None:
    return gen_data(rows, 1)

# @delete decorator return bool | None
@rd.delete(cl, prefix) 
def gen_data_delete(rows: int) -> bool | None:
    return gen_data(rows, 0)

Usage (Cache)

from timeit import timeit

# Flush keys
cl.flushall()

# First call - (no cache) - corresponding key: "test:100::"
print("No. cache - 100 row".ljust(20), timeit(lambda: gen_data_cache(100), number=1))
# No. cache - 100 row  1.0088350829901174

# Second call - (cache hit) - corresponding key: "test:100::"
print("Hit cache - 100 row".ljust(20), timeit(lambda: gen_data_cache(100), number=1))
# Hit cache - 100 row  0.002061583974864334

# Call with different arguments - (no cache) - corresponding key: "test:90::"
print("No. cache - 90 row".ljust(20), timeit(lambda: gen_data_cache(90), number=1))
# No. cache - 90 row   1.0119208749965765

# Second call - (cache hit) - corresponding key: "test:90::"
print("Hit cache - 90 row".ljust(20), timeit(lambda: gen_data_cache(90), number=1))
# Hit cache - 90 row   0.001857625029515475

# Data
print(gen_data_cache(100))
# <pandas.DataFrame>
# bool np_bool int int64 unit unit64 float ... bytes datetime datetime_tz time time_tz timedelta None
# 0 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 1 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 2 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 3 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 4 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# .. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
# 95 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 96 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 97 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 98 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# 99 True False 1 5 5 5 1.1 ... 2023-01-01 01:01:01.000001 2023-01-01 01:01:01.000001+08:00 01:01:01.000001 01:01:01.000001+08:00 1 days None
# [100 rows x 17 columns]

Usage (Update)

# Update existing cache - corresponding key: "test:100::"
print("Update cache - 100 row".ljust(20), timeit(lambda: gen_data_update(100), number=1))
# Update cache - 100 row 1.0083019589656033
print("Update status:", gen_data_update(100)) # Will return True since the key exists.
# Update status: True

# Update non-exist cache - corresponding key: "test:80::"
print("Update miss - 80 row".ljust(20), timeit(lambda: gen_data_update(80), number=1))
# Update miss - 80 row   0.00012520799646154046
print("Update status:", gen_data_update(80)) # Will return False since the key does not exist.
# Update status: False

# Data
print(gen_data_cache(100)) # Call cache function to retrieve the updated data
# <pandas.DataFrame>
# bool np_bool int int64 unit unit64 float ... bytes datetime datetime_tz time time_tz timedelta None
# 0 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 1 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 2 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 3 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 4 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# .. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
# 95 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 96 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 97 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 98 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# 99 True False 2 6 6 6 2.1 ... 2023-01-02 01:01:01.000001 2023-01-02 01:01:01.000001+08:00 02:01:01.000001 02:01:01.000001+08:00 2 days None
# [100 rows x 17 columns]

Usage (Delete)

# Delete existing cache - corresponding key: "test:100::"
print("Delete cache - 100 row".ljust(20), timeit(lambda: gen_date_delete(100), number=1))
# Delete cache - 100 row 0.00010604201816022396
print("Delete status:", gen_date_delete(100)) # Will return True since the key exists.
# Delete status: True

# Delete non-exist cache - corresponding key: "test:80::"
print("Delete miss - 80 row".ljust(20), timeit(lambda: gen_date_delete(80), number=1))
# Delete miss - 80 row   9.779195534065366e-05
print("Delete status:", gen_date_delete(80)) # Will return False since the key does not exist.
# Delete status: False

# Check cache - corresponding key: "test:80::"
print("Check key:", cl.get("test:100::"))
# Check key: None

Acknowledgements

redisdecor is based on several open-source repositories.

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

redisdecor-0.1.12.7.tar.gz (109.8 kB view details)

Uploaded Source

Built Distributions

redisdecor-0.1.12.7-cp312-cp312-win_amd64.whl (150.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

redisdecor-0.1.12.7-cp312-cp312-musllinux_1_1_x86_64.whl (397.9 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

redisdecor-0.1.12.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (398.4 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

redisdecor-0.1.12.7-cp312-cp312-macosx_10_9_x86_64.whl (154.1 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

redisdecor-0.1.12.7-cp312-cp312-macosx_10_9_universal2.whl (194.8 kB view details)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

redisdecor-0.1.12.7-cp311-cp311-win_amd64.whl (149.9 kB view details)

Uploaded CPython 3.11 Windows x86-64

redisdecor-0.1.12.7-cp311-cp311-musllinux_1_1_x86_64.whl (384.3 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

redisdecor-0.1.12.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (382.7 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

redisdecor-0.1.12.7-cp311-cp311-macosx_10_9_x86_64.whl (153.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

redisdecor-0.1.12.7-cp311-cp311-macosx_10_9_universal2.whl (194.2 kB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

redisdecor-0.1.12.7-cp310-cp310-win_amd64.whl (150.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

redisdecor-0.1.12.7-cp310-cp310-musllinux_1_1_x86_64.whl (358.8 kB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

redisdecor-0.1.12.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (351.8 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

redisdecor-0.1.12.7-cp310-cp310-macosx_10_9_x86_64.whl (153.9 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

redisdecor-0.1.12.7-cp310-cp310-macosx_10_9_universal2.whl (194.5 kB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file redisdecor-0.1.12.7.tar.gz.

File metadata

  • Download URL: redisdecor-0.1.12.7.tar.gz
  • Upload date:
  • Size: 109.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for redisdecor-0.1.12.7.tar.gz
Algorithm Hash digest
SHA256 f205abdc3ff17f341d912ed033c121d70eb2bc5989ae57d8c411ad05aa4f6d0f
MD5 cda6684fe7e8752a9a0a9091a27d7d3f
BLAKE2b-256 4bf9cfaba547cd4d2de448cb594433bb4eba2039cc4688c4d93292ee07e04c19

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fa4e8896d49b0e6f5157a63053a92a22ae52b3561b4abb3c95cb0f63d0b3570f
MD5 2c021b37aee9048893a21f6c4cad4c49
BLAKE2b-256 e35100820d0854c636c0632e0011d8532cefb1ae0798fc627255bf845c7ee678

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 bb9ad420c459597bb061e7be5f8c0ea02548ee98e0d9a0c8453ecd5551af9479
MD5 7cb86e2ce84f7d965c9ad2a3762a54a2
BLAKE2b-256 5aca699f9ef45d846536968bd4d95617a71894f5c2db35a30923c01b393b2a71

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 03bb7366ec866e00bf8c8b870b18811840e1d5c66e43027218110e37c6a26565
MD5 2c27839a89d4026682dad4830c9e8706
BLAKE2b-256 ad8cbf823c434282103f1a6be37ce0c83ab44884ce681e174000581116fc8028

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 efa38a0b69d5ece388699ca6c6ebe1fa7ea9621f87b8bca927b076a32c3ad076
MD5 2191ef714439b3ff2adb1e249da99a97
BLAKE2b-256 881eb8b87a8214b5fb75d5c9e75885536d94d564884e60f5a473e4bde1ebca18

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 30a7ff6586e66b419011392b9e763bdc255364dc8af19d7ebb791c390380e9c2
MD5 8468d5aa899165cc70a09a42146ece96
BLAKE2b-256 f2ae2d6a2743265768599510f2190cfcde52a31926993daba7e7e97f5f4050d4

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e16e14ac233eae140adbeedcb3284a3476e52cd294cc51007cb36466b1b62b4f
MD5 c96c30f7a5458401c7147929b3715d30
BLAKE2b-256 3997d1027c624fc6f06f420003237a1dbb86e4ba58036a83a1a0f669572a12ac

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 125e17ac0e2441833e7795c76f9437555c3b6032478f966beea0134b62269473
MD5 a229730653ad77ab5a62d32584056b0b
BLAKE2b-256 6e1d69421f4f9e735106ce5b9191422818a9dc2f967cae9f9659978c6c7a82dd

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d5142731bd5eddf07b3f3ad09c21b0788b0601ef73ddc3643b48bba4e88ef9a5
MD5 e91c34b45291cd41533b70b35fdce33b
BLAKE2b-256 5af92ef2235ed7671e425422c94e027834a3e0dc310be80d7fb61a33b449db9c

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 118f2d71e58084a67a9b4cfb0ecb8720cb52c4dd1da8f53f0cb8ae10c64c5bb3
MD5 8e055d1434e63b042bae09a1d8647eab
BLAKE2b-256 b2f7d4696416c125027c3a02ec00067bbb997bf85c7d1c5df7dda9cdf1c88cee

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 d93539bd5dba07747dd2247e79d46f4845de055de292e1bb8cde4afdbfc86d48
MD5 c900bd486218bca0374f2498086690c8
BLAKE2b-256 d122a42ad0f4ae320da55c18912c6a88afc25360de7f33dd10069f620d571465

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0dc585907be3b1c4386084cf6ef05e3dfeb95c0da677c16a25d2cded39372979
MD5 f84fa7d1f762d8b1d17e034a4cfe7742
BLAKE2b-256 70dfac210bf9f0069c6c825c1f16f3d6affa206ff40e8f882badd7e31e3dfdef

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2c3297abaa8e32cab8ec069b76f881a6b34b6e450ba1fd8bd1d16abb3bc62211
MD5 35d7e55cbec19ac067c6fd607e6455e9
BLAKE2b-256 4d799e4cb284b4753f4cb7d0a6d58bc52392abf14214d0597a17c501c08c7580

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8eaf30a32b15d45264c4944e42644baffd286cdea176a1cbb68b5ccee0a9fcdf
MD5 50d371db177c2aa8bdf799a30496c041
BLAKE2b-256 66c9c1470794e86f91176a043f1ebcf0b33be29579870663f5981841689df512

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ad80cd07ee25da4a767e23e22713ada85558e3a63ef1c04afe3162ebbd80d03b
MD5 74668aed190574699ac25f7f09e51721
BLAKE2b-256 3218dd607862c05df58cdccce229b318d5b9c5f33a3d544fa19ea3b4ab7cfa89

See more details on using hashes here.

File details

Details for the file redisdecor-0.1.12.7-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for redisdecor-0.1.12.7-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7b65c0f468814a92c86fb9ba2163549e6668b2a7e44f0d84fcedb1bfcc33fd9b
MD5 519c5834d11c12080d3d7887d1271fd1
BLAKE2b-256 9c0b2827bd0d5aca22c9e0cb6b35018154e618613fdc61f7b2fb5d2f0b85e2ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page