In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces what kind of way redis can use to achieve current limit, the article introduces in great detail, has a certain reference value, interested friends must read it!
Objective:
Implement access frequency restrictions
Realize that visitors $ip can only access $limit times within a certain amount of time $time
Non-script implementation
Private boolean accessLimit (String ip, int limit, int time, Jedis jedis) {boolean result = true; String key = "rate.limit:" + ip; if (jedis.exists (key)) {long afterValue = jedis.incr (key); if (afterValue > limit) {result = false;}} else {Transaction transaction = jedis.multi (); transaction.incr (key); transaction.expire (key, time); transaction.exec ();} return result;}
There are two defects in the above code.
There may be race conditions: the solution is to use WATCH to monitor the changes of rate.limit:$IP, but it is more troublesome; the above code needs to request up to 5 instructions from Redis without using pipeline, which is too much.
Lua script implementation
Redis allows Lua scripts to be passed to the Redis server for execution, most Redis commands can be called within the script, and Redis guarantees the atomicity of the script:
First you need to prepare the Lua code: script.lua
-Created by IntelliJ IDEA.-- User: jifang-- Date: 16Accord8Accord 24Murt-Time: 6:11-- local key = "rate.limit:". KEYS [1] local limit = tonumber (ARGV [1]) local expire_time = ARGV [2] local is_exists = redis.call ("EXISTS", key) if is_exists = = 1 then if redis.call ("INCR", key) > limit then return 0 else return 1 end else redis.call ("SET", key, 1) redis.call ("EXPIRE", key, expire_time) return 1 end
Java
Private boolean accessLimit (String ip, int limit, int timeout, Jedis connection) throws IOException {List keys = Collections.singletonList (ip); List argv = Arrays.asList (String.valueOf (limit), String.valueOf (timeout)); return 1 = = (long) connection.eval (loadScriptString ("script.lua"), keys, argv);} / load Lua code private String loadScriptString (String fileName) throws IOException {Reader reader = new InputStreamReader (Client.class.getClassLoader (). GetResourceAsStream (fileName)); return CharStreams.toString (reader);}
Lua embedded Redis advantages:
Reduce network overhead: code that does not use Lua needs to send multiple requests to Redis, while the script only needs to be once, reducing network transmission; atomic operation: Redis executes the entire script as an atom, there is no need to worry about concurrency, there is no need for transactions; reuse: the script will be permanently saved in Redis, other clients can continue to use.
The above is all the contents of the article "how can redis achieve current restriction?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.