Redis- data types

Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: an ordered set).

A, String (String)

redis string is the most basic type, you can be understood as type of exactly the same with Memcached, a key corresponding to a value.

The string type is binary safe. Redis meaning of string can contain any data. Such as jpg image or serialized object.

Redis basic string data type is a type, string type can store a maximum value of 512MB.

Examples

redis 127.0.0.1:6379> SET runoob "123天"
OK
redis 127.0.0.1:6379> GET runoob
"Rookie Tutorial"

In the above example we use Redis of  SET  and  GET  commands. Bond is runoob, corresponding to a value of  123 days .

Note: One of the biggest keys can store 512MB.

Two, Hash (hash)

Redis hash is a key (key => value) pairs.

Redis hash field is a string type and the value of the mapping table, hash is particularly suited for storing objects.

 

redis 127.0.0.1:6379> HMSET runoob field1 "Hello" field2 "World"
"OK"
repeat 127.0.0.1:6379> hget runoob field1
"Hello"
repeat 127.0.0.1:6379> hget runoob field2
"World"

Example We used the Redis  HMSET, HGET  command, HMSET  provided two field => value pair of, HGET obtain the corresponding  field  corresponding  value .

Each hash key 232-1 may be stored on (over 40 billion).

Three, List (list)

Redis list is a simple list of strings, sort insertion order. You can add an element to the head of the list (on the left) or tails (to the right).

Examples

redis 127.0.0.1:6379> THE runoob
repeat 127.0.0.1:6379> lpush runoob repeat
(integer) 1
redis 127.0.0.1:6379> lpush runoob mongodb
(integer) 2
repeat 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
repeat 127.0.0.1:6379> lrange runoob 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
repeat 127.0.0.1:6379>

List can store up 232--1 element (4,294,967,295, each list can store more than 4 billion).

Format: lpush name value

Add a string key element corresponding to the head of the list

Format: rpush name value

Add a string key element corresponding to the tail of the list

Format: lrem name value

key to delete the corresponding list to count and value the same element

Format: llen name  

Returns the key corresponding to the length of the list

Four, Set (collection)

Redis is a string Set the type of unordered collection.

Collection is achieved through a hash table, so add, delete, search complexity is O (1).

sadd command

Adding elements to set a string corresponding to the set of key, a successful return, if the element has been returned in the collection 0.

sadd key member

Examples

redis 127.0.0.1:6379> THE runoob
redis 127.0.0.1:6379> sadd runoob redis
(integer) 1
redis 127.0.0.1:6379> sadd runoob mongodb
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 0
repeat 127.0.0.1:6379> smembers runoob

1) "redis"
2) "rabitmq"
3) "mongodb"

Note: The above example rabitmq added twice, but according to a unique set of elements of the inserted second elements are ignored.

The maximum number of members for the collection 232--1 (4,294,967,295, each set can store 40 million members).

Five, zset (sorted set: an ordered collection)

Redis zset and is set as a collection of string type elements, and does not allow duplicate members.

The difference is that a double score will be associated with each type of element. It is to redis from small to large order of collection of member passing score.

Zset member is unique, but the score (score) it can be repeated.

zadd command

Is added to the collection element, the collection element is present in the corresponding score is updated

zadd key score member 

Examples

redis 127.0.0.1:6379> THE runoob
repeat 127.0.0.1:6379> Zadd runoob 0 repeat
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 mongodb
(integer) 1
repeat 127.0.0.1:6379> Zadd runoob 0 rabitmq
(integer) 1
repeat 127.0.0.1:6379> Zadd runoob 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> > ZRANGEBYSCORE runoob 0 1000
1) "mongodb"
2) "rabitmq"
3) "redis"

Each data type scenarios:

Types of Brief introduction characteristic Scenes
String (String) Binary Security May contain any data, such as jpg image or a sequence of objects, a maximum bond can store 512M ---
Hash (dictionary) A collection of key-value pairs, namely the type of programming language Map Suitable for storing objects, and may update the database as an attribute, as in a modification only one attribute value (in the Memcached need to remove the entire string deserialized into objects and then finished modified sequence of memory back) Store, read, modify user attributes
List (list) List (doubly linked list) Deletions fast, a certain period of operation of the API elements 1, updates ranking functions (such as circle of friends timeline) 2, message queue
Set (collection) Hash table implementation, the elements do not repeat 1, add, delete, search complexity is O (1) 2, provided for the intersection of the collection, and union, difference and other operations 1, 2 mutual friend, the use of unique, independent ip statistics all access to the site 3, when a friend recommended, according to the intersection of tag, is greater than a certain threshold can recommend
Sorted Set (ordered set) Set a weight increase of weight parameters score elements, the elements ordered by score When data is inserted into the collection, sorting has natural 1, 2 charts, message queues with weights
Published 21 original articles · won praise 0 · Views 2249

Guess you like

Origin blog.csdn.net/hfaflanf/article/details/103925252