Contact Us

Contact Us



Pleas confirm by checkbox


Technical

Python caching with redis

Author_img
By palasgaonkar December 20, 2016

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


 

Related posts
3 Crucial Ways of Inserting Code into a Running Application without Creating Bottlenecks
Technical

3 Crucial Ways of Inserting Code into a Running Application without Creating Bottlenecks

By palasgaonkar January 28, 2022
Apache Spark Standalone Setup On Linux/macOS
Technical

Apache Spark Standalone Setup On Linux/macOS

By palasgaonkar October 20, 2021
Apache Flink Standalone Setup on Linux/macOS
Technical

Apache Flink Standalone Setup on Linux/macOS

By palasgaonkar October 13, 2021
Identity, Authentication, And Access Management Using Azure Active Directory  
Technical

Identity, Authentication, And Access Management Using Azure Active Directory  

By palasgaonkar September 22, 2021
Things to Know Before You Select A Crypto Wallet
Blockchain

Things to Know Before You Select A Crypto Wallet

By palasgaonkar September 20, 2021
Solve 3 Most Irritating Outlook Email Rendering Issues.
Technical

Solve 3 Most Irritating Outlook Email Rendering Issues.

By palasgaonkar September 15, 2021
Intuit Wasabi – A Scalable A/B Testing Solution
Technical

Intuit Wasabi – A Scalable A/B Testing Solution

By palasgaonkar September 01, 2021
How To Pick The Right Data Analytics Strategy For Serverless Systems?
Big Data

How To Pick The Right Data Analytics Strategy For Serverless Systems?

By palasgaonkar August 25, 2021
Change Notifications and Named Options using Options pattern in .NET Core
Technical

Change Notifications and Named Options using Options pattern in .NET Core

By palasgaonkar August 18, 2021
Create Strongly Typed Configurations in .NET Core
Technical

Create Strongly Typed Configurations in .NET Core

By palasgaonkar August 13, 2021

Stay updated

Get the latest creative news from Fubiz about art, design and pop-culture.