redis basic data type usage

String

This is the most basic type, that is, normal set and get, do simple kv cache

 

hash

This is similar to the map with structure, and this is to be generally structured data, such as an object (provided that this object is not embedded in other objects) to cache redis, and then each time read and write cache, it can operation hash in a field.

key=42

value=

{

"id":42

“name":yaya

"age":20

}

Hash class data structure is mainly used to store a number of objects, some simple objects to be cached, when the subsequent operations, you can modify only the value of the object in a field

key=42

value=

{

"id":42

“name":yaya

"age":21

}

 

list

Ordered list, which can play a lot of tricks

Weibo, a big fan of v, you can go on redis cached list format

a big key = v

value = [quietly, Kiki, Yaya]

For example, by storing a list of some type of list data structures, similar to the fan list, review the list of things like

For example, by lrange command, is to start reading the number of elements from an element, you can implement paging list based on a query, this is a great feature, based on simple, high-performance redis page, you can do a similar kind of microblogging continue to drop down paging things, high performance, go from page to page.

Can engage in a simple example, message queue, into the dislike list head, tail where it comes out from the list;

 

set 

Unordered set, automatically de-emphasis; 

Based directly on the system need to set the weight of data lost in, automatically give go heavier. If you need some quick global data de-duplication, of course, you can go heavy on jvm memory of hashSet, but if you have a system deployed on multiple machines?

Based redis have globally set to obtain a weight;

  Can be set to play an intersection, union, difference-based operations, such as the intersection of it, you can put two people in a whole list of fans intersection, take a look at two common friend who yes. Right;

V The two large fans are placed in two set, the two set to make the intersection; if set to do the intersection on Baidu; api calls 

 

sorted set    zset

Ordered to re-set but can be ordered into the time to write a score, according to scores automatically sort, this can play a lot of tricks, the biggest feature is to have a score can be custom collations

For example, according to the time when if you sort the data, you can write into it by a certain time as the score, people automatically give you sort by time

Ranking:

zadd board 88 jingjing

zadd board 89 qiqi 

zadd board 90 yaya

zadd board 60 htion

Automatically sorted by score

90 yaya

89 qiqi

88 jingjing

60 htion

Next zrevange board 0 99, the user can get the top 100, zrank board username, you can see where the user ranking charts.

 zrevrange board 0 3

Users get top 3

90 yaya

89 qiqi

88 jingjing

Now is the second sort qiqi

zrank board qiqi

2

 

Guess you like

Origin www.cnblogs.com/H-hy/p/11304795.html