Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the uses of redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the usage of redis, which has a certain reference value, friends who need can refer to it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

1. High concurrency caching / shared session:

UserInfo getUserInfo (long id) {}

Fetch:

UserRedisKey = "user:info:" + id

Value = redis.get (userRedisKey)

If (value! = null) {

UserInfo = deserialize (value)

Return userInfo

}

Save:

UserInfo = * getUserInfo (id)

Redis.setex (userRedisKey, 3600, serialize (userInfo))

Using string to store serialized data is not stereoscopic and intuitive, but can be converted to hmset storage as a hash structure, and access is more intuitive.

two。 Simple distributed lock

Setnx can be set successfully only if it does not exist, and the rest can only wait. Single thread

3. Counter incr, because it is single-threaded, has less cpu consumption and higher performance than cas, etc.

Long incrVideoCounter (long id) {

Key = "video:playCount:" + id

Return redis.incr (key)

}

4. Implement stack / queue

Stack: lpush + lpop

Queue: lpush + rpop

5. Flow control / speed limit

PhoneNum = "12345678999"

Key = "shortMsg:limit:" + phoneNum

IsExists = redis.set (key, 1, "EX 60", "NX")

If (isExists! = null | | redis.incr (key)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report