Home Common Problem What is the use of redis

What is the use of redis

May 24, 2019 pm 05:10 PM
redis

The role of redis: 1. Get the latest n data; 2. Get the TOP N data; 3. Set the accurate rush time; 4. Implement the counter; 5. Remove duplicate values; 6. Use set Command to make an anti-spam system; 7. Build a queue system.

What is the use of redis

Many friends are confused after spending time learning the redis database. They feel empty and don’t know what they can do after learning this thing. As a result, I lost the motivation to continue learning. In order to allow everyone to continue moving forward on the road of learning redis, I think it is necessary to compile a simple and practical guide to redis application scenarios for everyone.

1. Get the latest n data

In order to let the friends understand better, I will explain it here with examples.

Example:

现在你在和讯网黄金频道上班,项目经理让你负责每天在频道首页生成一张数据表,来展示1个月内的国际黄金的开盘和收盘价格.如果
我们从mysql数据库中按时间排序取出,固然可以.但是你要考虑和讯网的并发量,和你排序时对资源的一个浪费程度.
Copy after login

At this time, it is necessary for us to choose the list access of redis. We know that redis has a queue (list) data structure, and it also has the first-in-first-out characteristic. In this case, I can get the latest 1-month data at my fingertips. A lrange command can simply take out the value of the interval you want.

#2. Get the TOP N data

The next example, looking at the title, feels similar to the first application, but there are actually some differences. In application 1, we use the first-in-first-out feature of the list to access according to the access time. Next Our example is to use the characteristics of ordered sets to solve practical problems.

现在你受雇于APPLESTORE,项目经理要求你在商店首页,展示下载量排行前30的APP.
Copy after login

At this time, if you can think of the SortedSet ordered set that we talked about during our redis learning process, then It couldn't be better. We only need to generate a unique ID for each APP, use it as a member in the collection, and use the download number as the member's score. Then when you insert this score into the ordered collection, redis will press After sorting by size, an ordered set will naturally be formed. In the end, it’s up to you to get the top N or bottom N data.

3. Set accurate rush buying time

Set rush purchase and promotion time. This technology is indeed very practical in the field of e-commerce.

现在你在阿里的淘宝事业部上班,领队组长分配你来完成一个限时抢购活动,某商品中午10:00开始进行抢购,1小时后,活动结束.
Copy after login

Hey, since the time is limited, it must end in exactly one hour. At this time we learn In the process of redis, the validity period of the key mentioned comes in handy. We can set the expiration time of an active key to the timestamp of the expiration moment. Concurrency is very high during the user's rush to buy. Then at this time We do not necessarily need to store the submitted data into our hard disk database immediately. When the user places an order, we obtain whether the key of the activity has expired. If it has not expired, the user is allowed to place the order, and the order data is temporarily cached in memory. Once the key expires, the user is prohibited from placing an order. At the same time, we can import as many concurrent orders as we just did into the hard disk database. First, we can achieve precise time setting, and secondly, we can cope with the high concurrency of rush buying.

4. Easily implement counters

你目前就职的单位是中央全面深化改革领导小组技术顾问办公室,在过去如果全国人民一起选代表,既浪费人力又浪费财力.现在网络技术
发展了,习近平组长交代由你牵头开发点赞选人大平台.
接到这个项目,你是不是很犯难,13亿啊!可能同时集中在某个时间段,点赞,如此大的IO请求.硬盘数据库多少有些力不从心.哎,这个时候
你要运用上我们的内存数据库系统了,redis中对key的增incre,减decr不正适合你的需求么.
Copy after login

5. Remove duplicate values

Remove duplicate values, it sounds familiar, our MySQL does not have a unique index Oh, well, what I want to say is that the set command for strings in redis is more useful.

你现在在下厨房平台,任职技术兼策划.你准备收集天下美食,苦于不知菜名.网络是有魔力的,人名群众的智慧是无穷的.你准备发动全
国的网友们,为你提供菜名,同时你还不想让大家发出相同菜名.
Copy after login

Seeing such a question, can you still think of the strings we were learning about redis before? Type, when talking about the set command, we talk about the default situation. When no other parameters are given, if our key does not exist, it will be created and added. If the key already exists, the old value overwrites the new value. In this case, the key value exists, Then it will be covered. Isn’t it the model we need?

6. Use the set command to create an anti-spam system

Many friends were confused when they saw this. When will you be able to fight spam by breaking the set command? Hey, you should be more imaginative, even bigger.

上次你制作的统计菜名系统,领导会上先把你肯定了一番,然后义正言辞地说,小明啊,你这系统不太符合净网行动的要求啊,有些网友把一
些带有明显淫秽色情的菜名都提交到了系统.还被展示出来投票,这不荒唐吗?
Copy after login

scared you to a cold sweat. At this time, you thought of studying in the high-performance architecture class of XX Education My deskmate Xiaopang. The Xiaopang told you on the phone that when we use the set command to operate a string, give it an NX parameter. In this way, the key will only be operated when the key submitted by the user does not exist. You Go to the Internet and download a dictionary of sensitive words and set it in advance. Then you can achieve anti-spam. After listening to Xiaopang’s guidance, you will be relieved when you are nervous.

7. Use publish and subscribe in SUBSCRIBE and PUBLISH commands build a real-time message push system

费了老鼻子劲,你终于进入了新浪微博事业部.第二天组长找到正埋头苦干的你.小明,咱们微博也要搞个跟FB一样一样的关注功能,搞大
V认证,大V一发消息,赶紧推送给关注的那群XX,让XX跟大V赶紧互动起来.以此来提高用户粘性.组长走后,”赶紧,推送”,这两个词在你的耳畔不时地回响.
Copy after login

You think of Xiaopang again. Xiaopang told you on the phone that there are subscribe and publish functions in redis. You hang up the phone and go After searching on the self-study IT website, it turned out as expected. A subscribe can subscribe to different user models. As long as the corresponding model has a publish implementation, the subscriber can receive push messages in real time. Although you are not Wang Dachui, you never expected it. Redis actually has such a function.O(∩_∩)Ohaha~

8. Build a queue system

忙完微博的事,正准备收拾东西去挤地铁的你.突然被组长叫住了.
小明,你虽然完成了微博关注和推送的技术架构.但是这几天又有个新问题出现了.因为FB用不了,微博用户暴增.特别是认证大V的用户越
来越多,就连门口烤红薯的大爷早上还拽着我说,给他整个大V吧,俺不差钱.老板为了缓解认证方面的压力,已经开始收费认证了,但是还是
挡不住这群XX.赶上过节饭点,大V们几乎同时发微博,推送服务器有点压力.你把这个问题解决了,再走吧,晚点回去打车公司报销.然后打
完卡他走了.
Copy after login

“同时发微博”,”同时推送,服务器压力大”.你从电脑中找到了小胖之前发给你的那本redis自学笔记,笔记上说.我们可以将若干个需要执行的事件加入队列,然后按时间的顺序去逐个逐个的执行.这个时候你又想到了一个提高用户互动性的好办法.我能不能让粉丝数目多的大V的微博优先推送呢?毕竟粉丝基数大,影响大,总能搞出一个大新闻.既然这样,我直接使用有序队列不就行了.把一个个大V的微博推送事件ID存入有序队列,同时依据粉丝数给予权重(或说优先级).不就可以达到我们的需求了么.

9. 缓存

这一点大家应该很熟悉了,它可以像memcache一样充当缓存数据库.不过的它的数据类型比memcache多的多,应对场景自然要比memcache丰富的多.比较时髦的用法是,用其来缓存会话哦.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to view the version number of redis How to view the version number of redis Apr 10, 2025 pm 05:57 PM

To view the Redis version number, you can use the following three methods: (1) enter the INFO command, (2) start the server with the --version option, and (3) view the configuration file.

What to do if redis-server can't be found What to do if redis-server can't be found Apr 10, 2025 pm 06:54 PM

Steps to solve the problem that redis-server cannot find: Check the installation to make sure Redis is installed correctly; set the environment variables REDIS_HOST and REDIS_PORT; start the Redis server redis-server; check whether the server is running redis-cli ping.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use redis zset How to use redis zset Apr 10, 2025 pm 07:27 PM

Redis Ordered Sets (ZSets) are used to store ordered elements and sort by associated scores. The steps to use ZSet include: 1. Create a ZSet; 2. Add a member; 3. Get a member score; 4. Get a ranking; 5. Get a member in the ranking range; 6. Delete a member; 7. Get the number of elements; 8. Get the number of members in the score range.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.