Skip to main content

Ct Redis Cache Lib

A specialized library developed by Credenti for efficient Redis caching in Flask applications.


Features

  • Decorator-based caching for Flask endpoints and functions
  • Configurable Redis connection pooling
  • Supports cache key templating with nested attribute access
  • Easy integration with Flask app lifecycle

Installation

pip install ct-redis-cache-lib

Configuration

Add the following to your Flask app configuration:

app.config['REDIS_CACHE'] = True              # Enable or disable Redis caching
app.config['REDIS_CACHE_EXPIRE_TIME'] = 60    # Default cache expiration time in seconds
app.config['CACHE_REDIS_HOST'] = 'localhost'  # Redis host
app.config['CACHE_REDIS_PORT'] = 6379         # Redis port
app.config['CACHE_CLIENT_NAME'] = 'my-client' # Redis client name

Usage

from flask import Flask
from ct_redis_cache_lib import init_cache
import logging

app = Flask(__name__)
logger = logging.getLogger(__name__)

# Initialize the Redis cache
ct_redis = init_cache(app, logger)

@app.route('/idle_time')
@ct_redis.cache(template_cache_key='idle_time_value:$idle_time', field='$idle_time')
def get_idle_time_value(idle_time: str):
    # Your function logic here
    return {'idle_time': idle_time}

Updating a Cache Entry

Update a cache entry directly:

ct_redis.update(
    cache_key='idle_time_value:123',  # Actual cache key
    field='some_field',
    value={'idle_time': 123},
    is_global=False,                  # or True if the key is global
    expire_time=120                   # optional, in seconds
)

Deleting a Cache Entry

Delete a cache entry:

ct_redis.delete(
    cache_key='idle_time_value:123',  # Actual cache key, not a template
    field='some_field',
    is_global=False                   # or True if the key is global
)

Committing and Resetting Cache

Commit cache writes after each request:

@app.after_request
def after_request(response):
    ct_redis.commit(response)
    return response

Reset cache if you need to roll back changes:

ct_redis.reset()

Cache Key Specification Examples

You can use dynamic cache keys by referencing function arguments or nested data using $.

Basic Example

@ct_redis.cache(template_cache_key='idle_time_value:$idle_time', field='$idle_time')
def get_idle_time_value(idle_time: str):
    return {'idle_time': idle_time}
  • If idle_time = 123, the cache key will be idle_time_value:123.

Nested Dictionary Example

@ct_redis.cache(template_cache_key='user_cache:$user.id', field='$user.id')
def get_user(user: dict):
    return user
  • If user = {'id': 01, 'name': 'User1'}, the cache key will be user_cache:01.

Nested Object Example

class User:
    def __init__(self, id, name):
        self.id = id
        self.name = name

@ct_redis.cache(template_cache_key='user_cache:$user.id', field='$user.id')
def get_user_obj(user: User):
    return {'id': user.id, 'name': user.name}
  • If user.id = 01, the cache key will be user_cache:01.

Deeply Nested Example

@ct_redis.cache(template_cache_key='deep_cache:$data.profile.info.id', field='$data.profile.info.id')
def get_deep(data: dict):
    return data
  • If data = {'profile': {'info': {'id': 01}}}, the cache key will be deep_cache:01.

Static Key Example

@ct_redis.cache(template_cache_key='static_key')
def get_static():
    return {'result': 1}
  • The cache key will always be static_key.

Metadata

Release files for ct-redis-cache-lib 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ct-redis-cache-lib 0.0.3
File Size Uploaded Provenance Actions
ct_redis_cache_lib-0.0.3.tar.gz 6.6 kB Without provenance Details for ct_redis_cache_lib-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ct-redis-cache-lib 0.0.3
File Interpreter ABI Platform Actions
ct_redis_cache_lib-0.0.3-py3-none-any.whl Python 3 none any Details for ct_redis_cache_lib-0.0.3-py3-none-any.whl

Total release size: 13.9 kB

Release files / ct_redis_cache_lib-0.0.3.tar.gz

Download URL ct_redis_cache_lib-0.0.3.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5aeb00ac44a5b889b1829eccbd021d969bfe2b805ee39c0502396c203b2aac82
BLAKE2b-256 checksum
How to use checksums
fd415559666535a909964bf9fee5e14082b11202da7e343c06795baac10896ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release files / ct_redis_cache_lib-0.0.3-py3-none-any.whl

Download URL ct_redis_cache_lib-0.0.3-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ea062bf5e61bb3ef75d9941981df7063b75899e3a5ba21b8a56e01a90965b53
BLAKE2b-256 checksum
How to use checksums
cb08f215c0748f6b3e31a2d5be03635bcda107bbaf1f466ae6c69de19aa38504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Security for ct-redis-cache-lib 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page