Skip to content

Redis Utilities

The Red class uses the official redis python package in its methods. Limeutils acts as a wrapper simplifying the use of the package and parsing all data it returns into valid python data types.

Quickstart

Get yourself up and running

from limeutils import Red

# Create the redis object
r = Red()

# STRING
r.set('message', 'Hello there')
r.get('message')                    # 'Hello there'
r.set('age', 5)
r.get('age')                        # 5 (int)
r.set('total', 12.5)
r.get('total')                      # 12.5 (float)

# HASH
r.set('user', dict(username='jimmy', age=99, gender='m'))
r.get('user')                               # dict(username='jimmy', age=99, gender='m')
r.set('user', dict(username='foo'))         # Update
r.get('user', only=['username', 'age'])     # dict(username='foo', age=99)

# LIST
r.set('names', ['jimmy', 'tina'])
r.set('names', ['sam'])
r.get('names')                              # ['jimmy', 'tina', 'sam']
r.get('names', start=1, end=-1)             # ['tina', 'sam']

# Save and read a set
r.set('names', {'jimmy', 'tina'})
r.get('names')                              # {'jimmy', 'tina'}

Setup

Use Default settings

from limeutils import Red

r = Red()

Use custom config

from limeutils import Red

CACHE_CONFIG = {
    'pre':  'FOOBAR',
    'ver':  'v1',
    'ttl':  3600,
}
r = Red(**CACHE_CONFIG)
  • pre: Prefix for your key. Defaults to an empty string.
  • ver: Version for your key. Defaults to an empty string.
  • ttl: Expires time for the key. Can be overridden in the set() method. Defaults to -1.
  • clear_wrongtype: Allows using an existing key for a different data type (e.g. string -> dict ). Defaults to True. Setting this to False raises an exception whenever you try saving with a different data type than the one you started with.

With connection information

from limeutils import Red

CACHE_CONFIG = {
    'host': 'localhost',
    'port': 6379,
    'db':   0,
    'pre':  'FOOBAR',
    'ver':  'v1',
    'ttl':  3600
}
r = Red(**CACHE_CONFIG)

If you don't include any connection information then the redis defaults will be used.

Key Prefixes

Limeutils lets you use prefixes for your keys allowing for better key management. Just add it when you create your object.

r = redis.Redis(pre='FOOBAR', ver='v1')

Keys are saved in the format prefix:version:key in redis. Creating a key named user in python is saved as FOOBAR:v1:user in redis. But when you need to use this key in python you only type the name user and the r object prepends the prefixes for you.