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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dgredis-0.1.2.tar.gz
Algorithm Hash digest
SHA256 98bb82e1b08d92abe909ef518df2a51be8e1ccdf0a8c66af72504c4616323cd7
MD5 ed68291f20f8e8f0c2594bd557c4d31c
BLAKE2b-256 9205e56337d1f7d74167bbf6610305aaed6b84fa1427553728196e2a3c56c082

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dgredis-0.1.2-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.5

File hashes

Hashes for dgredis-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab701218a79d8ce73368ba4092d6f695fda6a1ec0075a30830f94a6b596f3629
MD5 d7de35f37f0d5d7737e73359e9b9ef36
BLAKE2b-256 838469d506cc796df9baac9c827b7cb5dc21f7bd9f3cf899d3e8af76ba42f916

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