In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to add redis custom commands, the quality of the article content is high, so share it for everyone to make a reference, I hope you can gain something after reading this article.
Redis has become the standard cache of Internet companies due to its high-performance and lightweight excellent features. Sometimes we want to add some custom commands, mainly in scenes where Redis is heavily used, such as rush buying scenes. To ensure the transactional nature of multiple Redis commands, if there is no good atomicity guarantee, it is easy to have data inconsistency problems.
Although the official gives the transaction scheme, if the middle needs to judge according to the return value of a certain command before doing the next step, the transaction scheme cannot cope.
Today, we describe how to add a Redis command with a practical example, which is mainly used for anti-brush scenarios:
It is often necessary to ban an IP or a user for a period of time. If this command is not used, the scheme is as follows:
First incr, then determine whether it is 1, if it is 1, set the expiration time. You can see that this key does not expire if the first client that initiated the incr request hangs at high concurrency.
The command I'm going to write today is to ensure that this is transactionality, by setting the expiration time on the server side if key has a value of 1.
The command is used as follows:
incexpire key expireTime maxNum
key: key to be processed
Expiration Time: Expiration time in seconds. If 10 is written, it means that the key expires in 10 seconds.
maxNum: indicates how much it has not increased. If it is set to 10, the maximum value returned is 11. Returning 11 indicates that it has exceeded;
II. Writing orders
1. Download redis code
Redis version 3.2.11 used in this article;
Download the code and switch to the 3.2.11 branch
git clone https://github.com/antirez/redis
2. Write and add command code
1) Add a new command to the redisCommandTable struct in src/server.c
struct redisCommand redisCommandTable[] = {
redisCommandTable is an array, each item represents a redis command, where the first represents the name of the command, the second is the actual function processed, and the third is the number of parameters.
2) Add a function declaration to src/server.h:
void incrExpireCommand(client *c);
3) Then add a file called ljh.c (name yourself) and add the following code:
#include "server.h"
4) Add a new file ljh.o to src/Makefile
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking
The implementation effect is as follows
As you can see, key1 increases to 11 and stops increasing, and after 10 seconds, it becomes 1 again.
Finally, we still have to ask ourselves whether there is a need to add Redis commands in our scenario. If necessary, we should grasp the responsibilities between the various layers, and don't let Redis finally become a processing business. Generally speaking, the middleware layer generally handles more common functions. The more stable the lower layers, the less changes are normal.
The above is the way to add redis custom commands. Is there any harvest after reading it? If you want to know more about it, welcome to pay attention to industry information. Thank you for reading 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.