Get the App
SLTechnology News&Howtos  ›  Database  › 

What are the uses of redis

Shulou Source: shulou.com Published: 2022-06-01 02:42:39 10月12日 Update

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)

Tags: Data queues users buffers articles clusters blocking intuitive insufficient elements distributed at the same time commands size real-time files articles methods tags disks Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Redmi Xiaomi Shulou Tech Info