Ct Cache Lib
A specialized library developed by Credenti for efficient caching in Flask applications.
Features
- Decorator-based caching for Flask endpoints and functions
- Configurable Valkey connection pooling
- Supports cache key templating with nested attribute access
- Easy integration with Flask app lifecycle
Installation
pip install ct-cache-lib
Configuration
Add the following to your Flask app configuration:
app.config["CACHE"] = True # Enable or disable caching
app.config["CACHE_EXPIRE_TIME"] = 60 # Default cache expiration time in seconds
app.config["CACHE_HOST"] = "localhost" # Cache host
app.config["CACHE_PORT"] = 6379 # Cache port
app.config["CACHE_USERNAME"] = "user" # Cache username (if required)
app.config["CACHE_PASSWORD"] = "pass" # Cache password (if required)
app.config["CACHE_CLIENT_NAME"] = "my-client" # Cache client name
Usage
The @ct_cache.cache decorator can be applied to any function to enable caching. It supports both positional and keyword arguments.
You can also set read_only=True to prevent the cache from being updated on a cache miss.
This is useful for high-priority reads where you want to avoid writing to the cache.
from flask import Flask
from ct_cache_lib import init_cache
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
# Initialize the cache
ct_cache = init_cache(app, logger)
@app.route("/idle_time")
@ct_cache.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}
Bypassing the Cache
You can dynamically bypass the cache for any decorated function by passing use_cache=False when you call it. This tells the decorator to ignore the cache and execute the function directly.
# This call will use the cache
get_idle_time_value(idle_time="123")
# This call will bypass the cache and execute the function
get_idle_time_value(idle_time="123", use_cache=False)
Updating a Cache Entry
Update a cache entry directly:
ct_cache.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_cache.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
)
Deleting Cache Entries by Patterns
Delete cache entries that match one or more patterns. This is useful for bulk invalidation of related keys.
ct_cache.delete_by_patterns(
"user:123:*",
"session:*:active",
is_global=False, # or True if the patterns are global
batch_size=500 # optional, number of keys to delete in each batch
)
Committing and Resetting Cache
Commit cache writes after each request:
@app.after_request
def after_request(response):
ct_cache.commit(response)
return response
Reset cache if you need to roll back changes:
ct_cache.reset()
Cache Key Specification Examples
You can use dynamic cache keys by referencing function arguments or nested data using $.
Basic Example
@ct_cache.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 beidle_time_value:123.
Keyword Argument Example
The decorator also supports keyword arguments, making it flexible for various function signatures.
@ct_cache.cache(template_cache_key="user_cache:$user_id", field="$user_id")
def get_user_by_id(user_id: int):
# Function logic to fetch user
return {"user_id": user_id}
- If you call
get_user_by_id(user_id=42), the cache key will beuser_cache:42.
Nested Dictionary Example
@ct_cache.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 beuser_cache:01.
Nested Object Example
class User:
def __init__(self, id, name):
self.id = id
self.name = name
@ct_cache.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 beuser_cache:01.
Deeply Nested Example
@ct_cache.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 bedeep_cache:01.
Static Key Example
@ct_cache.cache(template_cache_key="static_key")
def get_static():
return {"result": 1}
- The cache key will always be
static_key.
Metadata
Release files for Sample-test-lib 0.10.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sample_test_lib-0.10.0.tar.gz | 8.2 kB | Without provenance | Details for sample_test_lib-0.10.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sample_test_lib-0.10.0-py3-none-any.whl | Python 3 | none | any | Details for sample_test_lib-0.10.0-py3-none-any.whl |
Total release size: 16.9 kB
Release files / sample_test_lib-0.10.0.tar.gz
| Download URL | sample_test_lib-0.10.0.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f47a480963250b989c3db134cc08ca126889fc23eb27e370067301aa61a53fc
|
|
BLAKE2b-256 checksum How to use checksums |
00f188e1bb71b8e94807e6c6784d3e09c7ea140397432388bec596871a1007b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Release files / sample_test_lib-0.10.0-py3-none-any.whl
| Download URL | sample_test_lib-0.10.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae39d690e180bba6f2684155b0b529f129999c1526a8834bb3e940be7526a7f3
|
|
BLAKE2b-256 checksum How to use checksums |
09e0b994ae771320baf158f2d7552def5178cca97c284e03b3d7a29fa3fa0173
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Security for Sample-test-lib 0.10.0
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.