Skip to main content
Latest PyPI version CI/CD Pipeline Documentation Status https://codecov.io/gh/datakortet/dkredis/branch/master/graph/badge.svg

A thin convenience wrapper around redis-py for storing Python values (via pickle), working with Redis hashes, performing atomic updates, and managing distributed locking primitives.

Installation

pip install dkredis

Usage

Connecting

from dkredis import dkredis

r = dkredis.connect()

connect() reads REDIS_HOST and REDIS_PASSWORD from environment variables, defaulting to localhost:6379.

Storing Python values

Any pickleable value can be stored and retrieved:

from dkredis.dkredis import set_pyval, get_pyval, pop_pyval

set_pyval('mykey', {'answer': 42}, secs=300)  # expires in 5 minutes
get_pyval('mykey')          # {'answer': 42}
pop_pyval('mykey')          # {'answer': 42}, then deletes the key

Dict / hash operations

Store and retrieve dicts as Redis hashes (string values only):

from dkredis.dkredis import set_dict, get_dict

set_dict('user:1', {'name': 'Alice', 'role': 'admin'}, secs=600)
get_dict('user:1')          # {'name': 'Alice', 'role': 'admin'}

Atomic updates

update() uses WATCH/MULTI pipelines for optimistic locking:

from dkredis.dkredis import update, setmax, setmin

r.set('counter', 40)
update('counter', lambda val: val + 2)  # atomically set to 42

setmax('highscore', b'100')  # r[key] := max(r[key], val)
setmin('lowscore', b'5')     # r[key] := min(r[key], val)

remove_if() atomically deletes a key only if it holds an expected value (implemented via a Lua script):

from dkredis.dkredis import remove_if

remove_if('mykey', expected_value)

Locking primitives

fetch_lock – prevents thundering-herd on cache misses. Only one process fetches fresh data; others receive False and should fall back to stale cache:

from dkredis.dkredislocks import fetch_lock

with fetch_lock('weatherapi', timeout=10) as should_fetch:
    if should_fetch:
        data = call_external_api()
        cache.set('weather', data, 60)
    else:
        time.sleep(1)
        data = cache.get('weather')

rate_limiting_lock – sets multiple keys atomically with MSETNX to enforce a per-resource cooldown period:

from dkredis.dkredislocks import rate_limiting_lock

if rate_limiting_lock(['smtp.example.com'], seconds=15):
    send_email()

mutex – a polling mutex using SETNX with expiry-based recovery:

from dkredis.dkredislocks import mutex

with mutex('mylock', seconds=30, timeout=60):
    # mutual exclusion zone
    ...

Multi-hash field lookup

mhkeyget() fetches one field from all hashes matching a key pattern:

from dkredis.dkredis import mhkeyget

mhkeyget('lock.*', 'x')
# {'lock.a': '1', 'lock.b': '2', 'lock.c': '3'}

Requirements

  • Python 3

  • redis==5.0.1

  • A running Redis server (localhost:6379 by default)

Metadata

Release files for dkredis 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dkredis 1.0.4
File Size Uploaded
dkredis-1.0.4.tar.gz 14.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dkredis 1.0.4
File Interpreter ABI Platform
dkredis-1.0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 26.1 kB

Release files / dkredis-1.0.4.tar.gz

Download URL dkredis-1.0.4.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8baa2a61bbd2bcca13e1f9e952ac14f215fe919e2ecc25c3de9b3791fbb7458c
BLAKE2b-256 checksum
How to use checksums
f9f3d750a39a8273ab15b848654923c5aa8d78b8cab24ec59aa04a2e4c612e8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release files / dkredis-1.0.4-py2.py3-none-any.whl

Download URL dkredis-1.0.4-py2.py3-none-any.whl
Size 11.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
bf63bc8478d8928f3c5c69d597f7922ae392b67f0ca23b4f81a8491d4f8b9640
BLAKE2b-256 checksum
How to use checksums
6f3663fc1d06ce3b3e3de9de65e48566fc86c546caeb7e55c30df65abdf7bb62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

1.0.5

2 release files

This release

1.0.4 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.5

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page