Python caching with redis

Redis is an in-memory data structure store, used as database, cache and message broker.

Installation –

Install python package

pip install redis

Usage –

Following code shows the basic operations such as saving, fetching & deleting objects.

import redis

r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
r.get('foo')
>>>'bar'
r.delete('foo')

So far, so good but this is only the beginning. Redis offers multiple functionalities in addition to what python-memcached offers.

Let’s take a look at the Redis data-structures and how helpful they are.

Data structures –

Redis Lists –

We can add elements to the Redis list data-structure by appending on left side or the right side.
While fetching the list we provide splicing indices just like in python (though both the indices are required in redis).

r.rpush('rlist', 1, 2, 3, 4, 5, 6, 7, 8, 9)
r.lrange('rlist', 0, -1)
>>>['1', '2', '3', '4', '5', '6', '7', '8', '9']

Using Redis lists means while modifying the cache after some operation, we can simply append to or delete from this list instead of the ‘delete old object & create new object with modified data’ approach with python-memcached. Nifty isn’t it?

We can restrict the length of this list with LTRIM command. So when used in conjuction with LPUSH command we can add elements to the list and always keep the latest N elements we want.

r.lpush('rlist', -1)
r.ltrim('rlist', 0, 1)
r.lrange('rlist', 0, -1)
>>>['-1', '1']

Here we see only the latest two integers are maintained in the cache object and rest are removed.

Redis Hashes –

Redis hashes are field-value pairs. Excellent for saving python dictionaries and modifying them in cache without having to recreate object in cache.

r.hmset('user', {'username': 'foo', 'birth_year': 1977})
# Fetch all field-value pairs in user key.
r.hgetall('user')
# Add additional field-value pairs.
r.hmset('user', {'user_id': 10})
# Increment values.
r.hincrby('user', 'user_id', 1)
# Fetch a single key.
r.hget('user', 'user_id')
>>>11
Redis Sets and Ordered Sets –

Just like in python redis Sets are useful for storing unique values, though if maintaining order is required then you’ll have to use Ordered Sets.
There are many other redis data-structures available. You can read more about them here.

Data persistence –

Redis also offers persistent storage. Be careful while using it though, since Redis only saves data after at least five minutes and 100 writes against the data set by default. So you might lose lose the latest minutes of data.

Redis also supports values up to 512 MB in size as opposed to 1 MB per key limitation of memcached.

Not only is redis the better option for places you might use python-memcached, it enables whole new types of use cases and usage patterns.

Hope this little intro to Redis was useful. For any comments/suggestions/questions you can find me at –
EMAIL – vishal.palasgaonkar@talentica.com
Skype – vishal.palasgaonkar_tal


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s