Home > Database > Redis > What are the functions of redis

What are the functions of redis

anonymity
Release: 2019-06-05 13:52:07
Original
13097 people have browsed it

Generally speaking, in terms of limitations, Redis also exists in the form of a message queue and as an embedded List to meet real-time high concurrency requirements. Usually in an e-commerce type data processing process, queues of related products, hot sales, and recommended sorting are usually stored in Redis, and the reading and updating of Redis lists by Storm are also included in the process.

What are the functions of redis

Redis is a high-performance key-value database.

Redis has the following three characteristics with other key-value caching products:

- Redis supports data persistence. It can save the data in the memory to the disk and can restart it again. Load for use.

- Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.

- Redis supports data backup, that is, data backup in master-slave mode.

Five uses:

1. Full page cache

Full page cache. If you're using server-side rendered content, you don't need to re-render each page for each individual request. Using a cache like Redis, you can cache frequently requested content, thereby greatly reducing latency for the most requested pages, and most frameworks have hooks for Redis caching pages.

// 设置全页面缓存1分钟
SET key "<html>...</html>" EX 60
// 获取全页面
GET key
Copy after login

2. Sequential arrangement

Because Redis’s Set data structure in memory can be incremented and decremented very quickly and efficiently, the performance is much higher than that of SQL queries. Compare this combined with Redis' sorted sets meaning you can grab the highest rated item in a list in milliseconds, and it's very easy to implement.

// 向排序集合中添加值
ZADD sortedSet 1 "one"
// 从排序集合中获取所有值
ZRANGE sortedSet 0 -1
// 从排序集合中获取所有值与权重
ZRANGE sortedSet 0 -1 WITHSCORES
Copy after login

3. Session Storage

The most common use of Redis I have seen is session storage. Unlike other session stores such as Memcache, Redis can persist data so that if the cache is stopped, on restart, all data is still there. Even if it's not a strictly ongoing task, this feature can still save your users a lot of trouble. No one likes to see their conversations randomly deleted for no reason.

// 设置一分钟过期的session
SET randomHash "{userId}" EX 60
// 获取用户ID
GET randomHash
Copy after login

4. Queuing

One of the less common, but very useful things you can do with Redis is queuing. Whether it's an email queue or data used by other applications, you can create an efficient queue in Redis. Any developer who is familiar with the stack and can push and pop projects can use this feature easily and naturally.

// 添加消息  
HSET messages <id> <message>
ZADD due <due_timestamp> <id>
// 接收消息
ZRANGEBYSCORE due -inf <current_timestamp> LIMIT 0 1  
HGET messages <message_id>
// 删除消息
ZREM due <message_id>
HDEL messages <message_id>
Copy after login

5.pub/sub

The ultimate real world use of Redis is the pub/sub that I will present in this article. This is one of the most powerful features built into Redis; the possibilities are endless. You can create a real-time chat system, trigger notifications for friend requests on social networks, and more. This feature is one of the most underrated features provided by Redis, but is extremely powerful and simple to use.

// 向通道中推送消息
PUBLISH channel message
// 从通道中获取消息
SUBSCRIBE channel
Copy after login

The above is the detailed content of What are the functions of redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:redis作用有哪些
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template