Talking about the Application of redis’ Data Structure

  redis

Order

This paper mainly studies the application of redis data structure

string

  • The most commonly used is incr operation, which can be used to maintain the number of raffle raffles remaining for a user in a lottery.
  • The setnx method can be used to implement distributed locks.

hashmap

  • Can be used to store sessions as an implementation of distributed sessions
  • Can be used to store the user’s shopping cart, the value value stores the key as an item and the value as its quantity

set

  • Set can be used to store the article id corresponding to each tag
  • It can also be used to store the voted user id of each article, and whether the value existed before can be judged by adding the return value.

zset

  • Zset can be used to store the votes of articles, use the votes as score, and use zset to sort the top n articles with the highest votes.
  • Or used to store the recently logged-in user id, using time as score, and using zset sorting to obtain the first n user ids recently logged in
  • You can also store the items the user has recently browsed, use time as score, and use zset to sort out the top n items the user has recently browsed.
  • You can also store the users who recently browsed the item, use the time as score, and use zset to sort out the top n users who recently browsed the item.

list

  • Can be used as a simple message queue, through the list of lpush and brpop as a message queue enqueue and consumption operations

hyperloglog

  • Used to roughly count the daily UV of websites.

geo(The bottom layer uses zset.)

  • Geo is used to store poi information, such as longitude and latitude of stores, and then store information of attachments can be queried according to radius.

bitmaps(The bottom layer is a string structure)

  • Used to count whether users log in everyday

Summary

Redis is more popular than memcache mainly because of its powerful data structure and the operations it provides. Rich data structure provides us with many conveniences in specific scenarios. A good and reasonable use of its data structure characteristics is the premise for using redis well.

doc