Monday, October 26, 2015

What, Why and How ?

What is Redis ?

Redis is an open-source in-memory data-structure which can be use as cache, broker and database. Redis support various data structure such as String, Hashes, List, Set and Sorted Set. Redis is written in ANSI C and production deployment is officially supported only on Linux versions.

Redis String operations
String operations are simplest form of operations which resembles with memcache operation. Memcache supports only string which Redis supports many more.

Commands for String operations (Using Redis CLI)
1. set <key> <value>
2. get <key> <value>
3. mset  <key> <value> <key> <value> <key> <value>
              multiple key-value set with single operation 
4. mget <key-1> <key-2> <key-3>
              multiple key fetch operation. If key is not present value returned will be 'nil'
5. set <key> <value> ex 10
             will set up key with expiry value of 10 sec

6. del <key>
7. expire <key> <seconds>
8. ttl <key>
              to know how much time is associated at present before key is going to expire. it is crucial to
              associate expiry with key in most of the situtations



Redis List operations
List maintained in redis is LinkedList which means that pushing in 10 or 1 million elements in the list is constant time operation. But searching list based on value is costly operation.

1. rpush <list-name> <value1> <value2> <value3> <value4>
            if list is not created, it will be created
2. rpop <list-name>
             pop will remove items from the list
3. lrange <list-name> <start-no> <no. of elements>
            no. of elements if valued for -1 means it will display all elements
            2 to -1 range will display all elements except two elements from head
4. llen <list-name>
            display size of no. of elements in list
5. lpush and lpop commands will push / pop elements on tail (left side)
6. brpop <list-name> <no-of-seconds-wait> this will add how much we will wait for operation
            if 0 seconds are specified it will be blocked for ever before next element is available.
7. exists <list-name>


Common Use cases for List :
1. To know last N elements on the list. e.g. Twitter display last N tweets by storing data in List data-type of Redis

Hash Operations

1. hset user:1000 username paresh
            Here, user:1000 is considered as key.
2. hget user:1000 username
3. hmset user:1000  birthyear 1975 version 1
4. hmget user:1000 birthyear version
5. hincrby user:1000 version 1 (will increment the version by 1)
6. exists user:1000
7. hgetall user:1000
8. hkeys user:1000
9. hexists user:1000 password (will return 0 as field password does not exists)
          to check whether field is existing in the given key or not


























No comments:

Post a Comment