Skip to main content

A convenient Redis client wrapper with JSON and datetime support.

Project description

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 RedisCoonfig
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

  1. Automatic serialization/deserialization:

    • datetime/date objects to/from ISO format
    • Complex structures (dict, list, tuple, set) to/from JSON
  2. Time-to-live (TTL) support for keys

  3. Support for both JSON documents (*_json_key methods) 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

  1. Redis server 4.0+ for JSON functionality
  2. Python 3.7+ (uses datetime.fromisoformat())

Limitations

  1. JSON operations require RedisJSON module
  2. Custom classes are not automatically serialized
  3. 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")

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

dgredis-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

dgredis-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file dgredis-0.1.0.tar.gz.

File metadata

  • Download URL: dgredis-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dgredis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2363602a341b160bbfbe4ea283f40d33381a7c54481c0d0219e24157a8b3f41f
MD5 c0a8531b1edcfc642c15503e6e484e27
BLAKE2b-256 0d0428348a729591e01c95ab0646ef700d95f0bc26223770e8bfd30583ad4a98

See more details on using hashes here.

File details

Details for the file dgredis-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dgredis-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dgredis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b48b0b7043d43068b7600c7c65d7d399001a0d9ddd4c78fe97855316da8ef3e
MD5 f13ab0645a5fb1fba9404e295e5a3938
BLAKE2b-256 8790163246f5c478bf1cd7eb3524f2d235665a82398abbd9561a5819e069f0e8

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