Skip to main content

Dictionary with Redis as storage backend

Project description

redis-dict

Build Status

A Python dictionary with Redis as the storage back-end. Redis is a great database for all kinds of environments; from simple to complex. redis-dict tries to make using Redis as simple as using a dictionary. redis-dict stores data in Redis with key-values, this is according to Redis best practices. This also allows other non-Python programs to access the data stored in Redis.

redis-dict was built out of the necessity of working with incredibly large data sets. It had to be possible to only send or receive the required data over the wire and into memory. With redis-dict it's as simple as a dictionary.

Example

Redis is a really fast database if used right. redis-dict uses Redis for key-value storage.

    >>> from redis_dict import RedisDict
    >>> dic = RedisDict(namespace='bar')
    >>> 'foo' in dic
    False
    >>> dic['foo'] = 42
    >>> dic['foo']
    42
    >>> 'foo' in dic
    True
    >>> dic["baz"] = "a string"
    >>> print(dic)
    {'foo': 42, 'baz': 'a string'}

In Redis our example looks like this.

127.0.0.1:6379> KEYS "*"
1) "bar:foo"
2) "bar:baz"

Features

Dictionary

redis-dict can be used as a drop-in replacement for a normal dictionary as long as no datastructures are used by reference. i.e. no nested layout e.g. values such list, instance and other dictionaries. When used with supported types, it can be used a drop-in for a normal dictionary.

redis-dict has all the methods and behavior of a normal dictionary.

Types

Several Python types can be saved and retrieved as the same type. As of writing, redis-dict supports the following types.

  • String
  • Integer
  • Float
  • Boolean
  • None

Expire

Redis has the great feature of expiring keys. This feature is supported.

  1. You can set the default expiration when creating a redis-dict instance.
r_dic = RedisDict(namespace='app_name', expire=10)
  1. With a context manager you can temporarily set the default expiration time. Defaults to None (does not expire)
seconds = 60
with r_dic.expire_at(seconds):
    r_dic['gone_in_sixty_seconds'] = 'foo'

Batching

Batch your requests by using Pipeline, as easy as using a context manager

Example storing the first ten items of Fibonacci, with one round trip to Redis.

def fib(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = (a+b), a

with r_dic.pipeline():
    for index, item in enumerate(fib(10)):
        r_dic[str(index)] = item

Namespaces

redis-dict uses namespaces by default. This allows you to have an instance of redis-dict per project. When looking directly at the data in Redis, this gives you the advantage of directly seeing which data belongs to which app. This also has the advantage that it is less likely for apps to collide with keys, which is a difficult problem to debug.

More Examples

More complex examples of redis-dict can be found in the tests. All functionality is tested in either assert_test.py (here) or in the unit tests (here).

Installation

pip install redis-dict

Note

This project is used by different companies in production.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for redis-dict, version 1.5.2
Filename, size File type Python version Upload date Hashes
Filename, size redis_dict-1.5.2-py3-none-any.whl (5.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size redis dict-1.5.2.tar.gz (5.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page