Redis Data Type: Basic and advance

There are 5 kinds of statistics sorts supported through Redis database.

Strings

Hashes

Lists

Sets

Sorted Sets

Strings

String is a set of bytes. In Redis database, strings are binary safe. It capability they have a recognized length and no longer decided by means of any specific terminating characters. So it is feasible to shop whatever up to 512 megabytes in one string.

Example

Let’s save a string name “Ajeet Kumar” in the key via using SET command and then retrieve the identical by the usage of GET command.

SET name "Ajeet Kumar"  
OK  
GET name   
"Ajeet Kumar"  

In the above example, SET and GET are the Redis command, identify is the key used in Redis, “Ajeet Kumar” is string value stored in Redis.

Hashes

Hash is a collection of key-value pairs. In Redis, hashes are maps between string fields and string values. So, they are used to symbolize objects.

Example

Let’s store a user’s object which contains basic records of a user.

HMSET user:1 username ajeet password javatpoint alexa 2000  
OK  
HGETALL  user:1  
"username"  
"ajeet"  
"password"  
"javatpoint"  
"alexa"  
"2000"  

Here, HMSET and HGETALL are the command for Redis, whilst user:1 is the key.

Every hash can store up to 232 – 1 field-value pairs (more than 4 billion).

Lists

Redis Lists are described as a lists of strings, sorted by means of insertion order. You can add elements to a Redis List on the head or on the tail.

Example

lpush javatpoint java  
(integer) 1  
lpush javatpoint java  
(integer) 1  
lpush javatpoint java  
(integer) 1  
lpush javatpoint java  
(integer) 1  
lrange javatpoint 0 10  
"cassandra"  
"mongodb"  
"sql"  
"java"  

The max size of a list is 232 – 1 factors (more than four billion of elements per list).

Sets

Sets are an unordered series of strings in Redis database. In Redis, you can add, remove, and check for the existence of members in O(1) time complexity.

Example

sadd tutoriallist redis  
(integer) 1  
redis 127.0.0.1:6379> sadd tutoriallist sql  
(integer) 1  
redis 127.0.0.1:6379> sadd tutoriallist postgresql  
(integer) 1  
redis 127.0.0.1:6379> sadd tutoriallist postgresql  
(integer) 0  
redis 127.0.0.1:6379> sadd tutoriallist postgresql  
(integer) 0  
redis 127.0.0.1:6379> smembers tutoriallist  
1) "redis"  
2) "postgresql"  
3) "sql"  

In the above example, you can see that postgresql is brought thrice but due to special property of the set it is delivered only once.

The max wide variety of participants in a set is 232 – 1 elements (more than four billion of elements per list).

Sorted Sets

Redis Sorted Sets are similar to Redis Sets. They are additionally a set of non-repeating collections of Strings. But each and every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, the ratings may also be repeated.

Example

redis 127.0.0.1:6379> zadd tutoriallist 0 redis  
(integer) 1  
redis 127.0.0.1:6379> zadd tutoriallist 0 sql  
(integer) 1  
redis 127.0.0.1:6379> zadd tutoriallist 0 postgresql  
(integer) 1  
redis 127.0.0.1:6379> zadd tutoriallist 0 postgresql  
(integer) 0  
redis 127.0.0.1:6379> zadd tutoriallist 0 postgresql  
(integer) 0  
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 10  
1) "postgresql"  
2) "redis"  
3) "sql"  

Next TopicRedis All Commands