Redis Client Wrapper
A convenient Redis client wrapper with JSON and datetime support.
Installation
Requires Redis server and redis-py library:
pip install redis
Usage
Initialization
from dgredis.conf import RedisConfig
from dgredis import RedisClient
config = RedisCoonfig(
host="localhost",
port=6379,
password=None
)
client = RedisClient(config)
Core Methods
JSON Operations
# Store JSON data
data = {"name": "John", "date": datetime.now()}
client.set_json_key("user:1", data, ttl=3600)
# Retrieve JSON data
result = client.get_json_key("user:1")
Basic Key Operations
# Set value
client.set_key("last_update", datetime.now(), ttl=60)
# Get value
last_update = client.get_key("last_update")
Key Type Check
key_type = client.get_key_type("user:1")
Features
-
Automatic serialization/deserialization:
- datetime/date objects to/from ISO format
- Complex structures (dict, list, tuple, set) to/from JSON
-
Time-to-live (TTL) support for keys
-
Support for both JSON documents (
*_json_keymethods) and simple keys
Technical Details
Serialization Behavior
- Dates and datetimes are stored in ISO8601 format (UTC)
- Nested structures are automatically handled recursively
- Original Redis types are preserved when possible
Error Handling
- Invalid dates fall back to string values
- JSON decode errors return raw values
Requirements
- Redis server 4.0+ for JSON functionality
- Python 3.7+ (uses datetime.fromisoformat())
Limitations
- JSON operations require RedisJSON module
- Custom classes are not automatically serialized
- Timezone-naive datetimes are assumed to be in local timezone
Examples
Working with Nested Structures
data = {
"user": "Alice",
"login_dates": [datetime(2023,1,1), datetime(2023,1,15)],
"preferences": {"dark_mode": True}
}
client.set_json_key("user:alice", data)
TTL Management
# Set key with 5 minute expiration
client.set_key("temp_data", {"value": 42}, ttl=300)
# Check remaining TTL (using native redis client)
remaining_ttl = client.client.ttl("temp_data")
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dgredis-1.0.1.tar.gz
(7.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dgredis-1.0.1.tar.gz.
File metadata
- Download URL: dgredis-1.0.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5aa3422a818130975bb3ff3394907c29581364146922a0ec0c6c3db4acac867b
|
|
| MD5 |
dcdfc322c2ecc43b2f2543ff808004d2
|
|
| BLAKE2b-256 |
ca52e4bbbe6b538adea0c95cf494450c7c52ccb27a02d49e95ea34a831ba2a12
|
File details
Details for the file dgredis-1.0.1-py3-none-any.whl.
File metadata
- Download URL: dgredis-1.0.1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef9bb5fd38d31ce5b03eb662c29154fa5cdbb6268f36f329418629428a05ed84
|
|
| MD5 |
661c0d049c693e6667e08f9ec56b169c
|
|
| BLAKE2b-256 |
b995f39635281d4981a7ffe4e4ca1628eb8b2f21ed022e82f5fe1182ec5c0e36
|