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-1.0.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

dgredis-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dgredis-1.0.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dgredis-1.0.0.tar.gz
Algorithm Hash digest
SHA256 34f634ac1d18728b584c3072209077d75987a6049e44db30661e066eb034de4d
MD5 66cd8e5387f9bcf97a84e3e42fbfc1e9
BLAKE2b-256 4a0246ed6d7a3204176ea05d05f350ea98723cafe06e678cbcccee5a66276f30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dgredis-1.0.0-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

Hashes for dgredis-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4326ad2494e5eea3f66fb6b61aa8618ecc80aa864b6fe5e5dc766487e2d9bf58
MD5 a07cdd333be5a67969f052d119689a5d
BLAKE2b-256 81ac423a565046a9155ba51e0480be676a2107e1b940bf0d232418892f370174

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