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

  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.1.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.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dgredis-0.1.1.tar.gz
Algorithm Hash digest
SHA256 de0cf873a9edfe65deb91bac949a8600b0d48e7ae5f3ee26e8ed2308980de656
MD5 fe96780399274b989add28e3076f9732
BLAKE2b-256 ef0c560069b614772e69c476152485c0d59aaac9d21007117575b932beda822b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dgredis-0.1.1-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.3

File hashes

Hashes for dgredis-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e399c90a6f2d3ad8a64c3034f89437c0d87f21de8f59895e27d4d5a4ad997715
MD5 3e8508ab0cd35c03cab9c01650ad1597
BLAKE2b-256 311b13a70c3349ded46f0b756de159308f54ca3560e37b8906d82bc2e323791d

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