Redis Cache Library
Overview
This is a simple and efficient Redis caching library for Python that supports Active TTL. It provides an easy-to-use interface for storing, retrieving, and managing cached data while automatically refreshing TTL for active records (if enabled). The library is designed for caching any type of data, not just user sessions, making it a versatile solution for various use cases.
Features
- Dictionary-style access (
cache[key] = valueandcache[key]) - Auto field updates via
update() - Bulk insert shortcut via
add() - Configurable Redis Credentials via
config.py - Custom TTL per Record (default and per-key support)
- Optional Active TTL (Resets TTL when a record is accessed or updated if enabled)
- Efficient Background Cleanup for stale records
- Simple API for CRUD Operations
Installation
pip install rcache-live
Usage
Initialize the Cache
from rcache_live import RCacheLive
# Create a Redis cache instance (Active TTL is disabled by default)
cache = RCacheLive()
# Create a Redis cache instance with Active TTL enabled
cache = RCacheLive(active_ttl=True)
Set and Get a Record (Dictionary-Style Access)
cache["test_key"] = {"name": "Alice"} # Set a record
print(cache["test_key"]) # Get a record
Auto Field Updates
cache["test_key"] = {"name": "Alice", "age": 25}
cache.update("test_key", age=26)
print(cache["test_key"]) # Output: {'name': 'Alice', 'age': 26}
Bulk Insert Shortcut
cache.add(key1={"data": 1}, key2={"data": 2})
print(cache["key1"]) # Output: {'data': 1}
print(cache["key2"]) # Output: {'data': 2}
Delete a Record
cache.delete_record("test_key")
print(cache.get_record("test_key")) # Output: None
Get All Records
all_records = cache.get_all_records()
print(all_records)
Cleanup Inactive Records
cache.cleanup_inactive_records(past_minutes=0.1)
Active TTL - Unique Feature (Optional)
What is Active TTL?
Active TTL is a feature that automatically extends the lifespan of a record when it is accessed or updated. This ensures that frequently used records remain in the cache while inactive records expire naturally.
How Does It Work?
- When you get a record, its TTL is reset (only if
active_ttl=True). - When you update a record, its TTL is also refreshed.
- The library maintains a separate tracking mechanism to efficiently update TTL without modifying the full record.
Example Usage
# Enable Active TTL when initializing the cache
cache = RCacheLive(active_ttl=True)
# Set a record with Active TTL enabled
cache["session:456"] = {"user": "Alice", "status": "active"}
# Accessing the record within the TTL period will extend its TTL
user_session = cache["session:456"]
print(user_session) # {'user': 'Alice', 'status': 'active'}
Why Use Active TTL?
✅ Prevents unnecessary cache evictions for frequently used data ✅ Ensures session-like behavior (e.g., user sessions, API response caching, temporary data) ✅ Users don't need to manually manage TTL; it is handled automatically ✅ Set TTL once, and Active TTL will extend it dynamically based on access
Lazy Cleanup Mechanism
What is Lazy Cleanup?
Lazy cleanup ensures that expired or inactive records are removed in an efficient manner without causing unnecessary Redis load.
Example Usage
# Run cleanup to remove inactive records older than 6 seconds
cache.cleanup_inactive_records(past_minutes=0.1)
Checking Cache Stats - New!
# Run the command in CLI
rcache-live-stats
Sample Run to see usage
To test the functionality of rcache-live, use the run_sample.py file:
Clone the repository and run the following commands:
git clone https://github.com/yourusername/rcache-live.git
cd rcache-live
python sample_test.py
Developer Guide
Running Tests
The test cases are included in the repo, developers should run the tests before raising a new pull request
To run the tests:
```sh
pytest tests/
License
This project is licensed under the MIT License.
Metadata
Release files for rcache-live 0.2.5
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 |
|---|---|---|---|---|
| rcache_live-0.2.5.tar.gz | 7.3 kB | Without provenance | Details for rcache_live-0.2.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rcache_live-0.2.5-py3-none-any.whl | Python 3 | none | any | Details for rcache_live-0.2.5-py3-none-any.whl |
Total release size: 15.5 kB
Release files / rcache_live-0.2.5.tar.gz
| Download URL | rcache_live-0.2.5.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
13b42b1632fc863dc2fe399c2cb8968d1fa03a0d9a16821a304a4a8021972f6d
|
|
BLAKE2b-256 checksum How to use checksums |
54237960e2bf8f46011db017dcd84515ae7e6b954ff8d781eeecc32ac19420a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / rcache_live-0.2.5-py3-none-any.whl
| Download URL | rcache_live-0.2.5-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
608a0f852613d05f58e9a051643feb6e501b990332a92edc919c50e134cc3d11
|
|
BLAKE2b-256 checksum How to use checksums |
74d8207215212b29d11277c6ce5cee93da5f67691e3dae800d40cdf865fbecff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Security for rcache-live 0.2.5
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.