Use Hash/Dictionary structure in Redis

LAI TOCA
2 min readNov 25, 2019

--

Reference from: https://www.cloudfoundry.org/the-foundry/redis-for-pcf/

Sometimes we would like to store our class object to Redis as cache for further process for quick lookup or restore purposes like below screen shot. Assume we have a class called: Member, and we have created data structure dictionary inside the Redis as contract book.

Class object store to Redis in hash/dictionary structure
contract book as hash structure
The benefit of choose hash/dictionary for storing our data was we could have O(1) to operate (Get/Update) specific record(s) if you got proper key for it😉.

Here was the sample code in that we could achieve to drop our class collection into Redis with hash/dictionary structure:

You might wonder that why we using batch way for operating with Redis server? Below was simple benchmark for dealing 5000 records:

Benchmark running for batch insert VS iterator insert
You could find out that batch insert would take advantage of reduce amount of I/O (networking) so each round for processing 5000 records almost less than 2 seconds comparing to traditional iterator way that cost more than 20 seconds per round. The batch method proceed with 10 times faster👍

If you got interested with benchmark source, see below:

If you got interested in how to do it via lua script, please refer to this story😉.

Reference

--

--

LAI TOCA
LAI TOCA

Written by LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)

No responses yet