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 else can redis do besides caching?

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

Share

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

This article is about what redis can do in addition to caching. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

Redis should be said to be one of the most popular NoSQL databases today. Redis is usually used as a caching component to cache data. However, in addition to caching data, there is a lot more Redis can do. The following are a few examples for your reference.

1. The latest list

For example, the latest news list on the news list page, if the total number is very large, try not to use select a from A limit 10, try redis's LPUSH command to build List, and stuff it one by one. But what if the memory is cleared? Also simple, if the query can not store key, use mysql query and initialize a List to redis.

2. Ranking application

The main redis data type used to implement this function is zset, an ordered collection of redis. Zset is an extension of the set type, which has one more order attribute than the original type. This attribute automatically adjusts the order values each time the data is inserted to ensure that the values are arranged continuously in a certain order.

Let's assume that it is a ranking of game experience values, then the main implementation idea is:

When a new player participates in the game, add a record in zset in redis (the record content depends on the specific requirements) score is 0

When the player's experience value changes, modify the player's score value

Use the ZREVRANGE method of redis to get the ranking

3. Counter application

Redis commands are atomic, and you can easily use INCR and DECR commands for atomic operations to build a counting system. Because of single threading, concurrency problems can be avoided, no errors can be guaranteed, and 100% millisecond performance.

For example, in a web application, if you want to know the number of clicks per day of the year, you can simply use the user's ID and related date information as keys and perform a self-increment operation each time the user clicks on the page.

4. Data weighting

Redis set can automatically arrange weights, when you need to store a list of data, but do not want to duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set collection.

Implementation plan:

The internal implementation of set is a HashMap in which value is always null. In fact, the weight is arranged quickly by calculating hash, which is why set can determine whether a member is in the collection or not.

5. Real-time anti-garbage system

Anti-spam systems are usually based on keywords, using Redis to store relational words, which can take advantage of the high performance of Redis to provide stable and accurate real-time monitoring functions for the monitoring system, such as mail system, comment system and so on.

6. Real-time messaging system that can publish and subscribe

Pub/Sub systems in Redis can build real-time messaging systems, for example, many real-time chat applications built using Pub/Sub.

Design ideas:

The server sends messages (including title and content), the title is stored in redis according to certain rules, and the title (with the least amount of information) is pushed to the client. When the client clicks on the title, it gets the corresponding content to read.

If you don't read it, you can tell how many items are unread. Redis can count quickly.

Clean the cache according to a certain amount of time

Technical implementation:

Need redis database, client-side websocket, server-side websocket

Thank you for reading! This is enough about what redis can be used to do in addition to caching. I hope the above content can be helpful to you so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

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