Skip to main content

A simple and efficient Redis caching library with Active TTL support

Project description

Redis Cache Library

PyPI version

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] = value and cache[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.rcache_handler 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 dataEnsures session-like behavior (e.g., user sessions, API response caching, temporary data)Users don't need to manually manage TTL; it is handled automaticallySet 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)

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.

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

rcache_live-0.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rcache_live-0.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file rcache_live-0.2.0.tar.gz.

File metadata

  • Download URL: rcache_live-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for rcache_live-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d7d087fb8d71f1d6b8f815c0183c5dc1983b469ea5a0805ef2ff9bee98eebd3c
MD5 b1cb44d58edc13f4f2427b0baf19b1b3
BLAKE2b-256 86be3565cbac580a55134ad5f45deabcae65b5b30fa8afa9d53c3f117413438f

See more details on using hashes here.

File details

Details for the file rcache_live-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rcache_live-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for rcache_live-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df7361fdf3509d88d2f3b1dc369a4c38b8fe7b1e6a77b49fde2a7f902c73944a
MD5 cf7224c1f20c1a770182f0b2e1bb81de
BLAKE2b-256 48a501012f71f1bf52a375e666f823ff2fbdf0bb5cbcb579e8afbdfd06fa73b6

See more details on using hashes here.

Supported by

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