Redis - Data Types
Redis supports 5 types of data types, which are described below:
Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters, so you can store anything up to 512 megabytes in one string.
redis 127.0.0.1:6379> SET domain "w3clan.com" OK redis 127.0.0.1:6379> GET domain "w3clan.com"
In the above example SET and GET are redis commands, domain is the key used in redis and w3clan.com is the string value that is stored in redis.
NOTE: A String value can be at max 512 Megabytes in length.
A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values, so they are used to represent objects
redis 127.0.0.1:6379> HMSET user1 username w3clan password w3clan.com course redis.w3clan.com OK redis 127.0.0.1:6379> HGETALL user1 1) "username" 2) "w3clan" 3) "password" 4) "w3clan.com" 5) "course" 6) "redis.w3clan.com"
In the above example hash data type is used to store user's object which contains basic information of user. Here HMSET, HGETALL are commands for redis while user1 is the key.
Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to a Redis List on the head or on the tail.
redis 127.0.0.1:6379> lpush storageType redis (integer) 1 redis 127.0.0.1:6379> lpush storageType mongodb (integer) 2 redis 127.0.0.1:6379> lpush storageType nosql (integer) 3 redis 127.0.0.1:6379> lrange storageType 0 3 1) "nosql" 2) "mongodb" 3) "redis"
The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).
Redis Sets are an unordered collection of Strings. In redis you can add, remove, and test for existence of members in O(1) time complexity.
redis 127.0.0.1:6379> sadd storageTypo redis (integer) 1 redis 127.0.0.1:6379> sadd storageTypo mongodb (integer) 1 redis 127.0.0.1:6379> sadd storageTypo nosql (integer) 1 redis 127.0.0.1:6379> sadd storageTypo nosql (integer) 0 redis 127.0.0.1:6379> smembers storageTypo 1) "nosql" 2) "mongodb" 3) "redis"
NOTE: In the above example nosql is added twice but due to unique property of set it is added only once in storageType key.
The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).
Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
redis 127.0.0.1:6379> zadd storageTypes 0 redis (integer) 1 redis 127.0.0.1:6379> zadd storageTypes 0 mongodb (integer) 1 redis 127.0.0.1:6379> zadd storageTypes 0 nosql (integer) 1 redis 127.0.0.1:6379> zadd storageTypes 0 nosql (integer) 0 redis 127.0.0.1:6379> ZRANGEBYSCORE storageTypes 0 10 1) "redis" 2) "mongodb" 3) "nosql"
Related Results :
- Related Posts are generally User Blog posts.
- or Other tutorials from other networks of w3clan.com.
- Any registered user can create related posts based on search term tags.