Home >Web Front-end >JS Tutorial >Analysis of 5 common application scenarios of Redis

Analysis of 5 common application scenarios of Redis

小云云
小云云Original
2018-02-28 15:11:177098browse

If you have the impression that Redis is just a key-value storage, then you have missed many powerful functions of Redis. Redis is a powerful memory storage with rich data structures, which can be used in many aspects, including As a database, cache, message queue, etc.

1. Full page caching

If you are using server-side content rendering and you don’t want to re-render each page for each request, you can use Redis to cache frequently requested content Caching can greatly reduce the delay of page requests. Many frameworks already use Redis to cache pages. This is a way to staticize pages.

// Set the page that will last 1 minute
SET key "<html>...</html>" EX 60
// Get the page
GET key

2. Ranking

Redis is based on memory and can handle increasing and decreasing operations very quickly and efficiently. Compared with the processing method of using SQL requests, the performance improvement is very huge. .

Redis' ordered collection can easily implement "get the top N elements from a large list" in milliseconds, and it is very simple.

// Add an item to the sorted set
ZADD sortedSet 1 "one"
// Get all items from the sorted set
ZRANGE sortedSet 0 -1
// Get all items from the sorted set with their score
ZRANGE sortedSet 0 -1 WITHSCORES

3. Session storage

This may be the most widely used point. Compared with session storage similar to memcache, Redis has the ability to persist cache data. When the cache fails due to problems After restarting, the previous cached data is still there, which is more practical and avoids user experience problems caused by the sudden disappearance of the session.

// Set session that will last 1 minute
SET randomHash "{userId}" EX 60
// Get userId
GET randomHash

4. Queue

For example, the email sending queue and the data queue waiting to be consumed by other applications. Redis can easily and naturally create an efficient queue.

// Add a Message
HSET messages <id> <message>
ZADD due <due_timestamp> <id>
// Recieving Message
ZRANGEBYSCORE due -inf <current_timestamp> LIMIT 0 1
HGET messages <message_id>
// Delete  Message
ZREM due <message_id>
HDEL messages <message_id>

5. Publish/Subscribe

pub/sub is a very powerful feature built into Redis. For example, it can create a real-time chat system, notification triggers in social networks, etc.

// Add a message to a channel
PUBLISH channel message
// Recieve messages from a channel
SUBSCRIBE channel

Related recommendations:

PHP uses Redis examples to explain

PHP about the definition and usage of redis counter class

Scenarios where Redis needs to be used in PHP projects

The above is the detailed content of Analysis of 5 common application scenarios of Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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