Home >Common Problem >What is the use of redis

What is the use of redis

(*-*)浩
(*-*)浩Original
2019-05-24 17:10:0542958browse

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数据库中按时间排序取出,固然可以.但是你要考虑和讯网的并发量,和你排序时对资源的一个浪费程度.

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.

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小时后,活动结束.

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不正适合你的需求么.

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.

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

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.

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

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赶紧互动起来.以此来提高用户粘性.组长走后,”赶紧,推送”,这两个词在你的耳畔不时地回响.

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们几乎同时发微博,推送服务器有点压力.你把这个问题解决了,再走吧,晚点回去打车公司报销.然后打
完卡他走了.

“同时发微博”,”同时推送,服务器压力大”.你从电脑中找到了小胖之前发给你的那本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:
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
Previous article:Why is redis query fast?Next article:Why is redis query fast?