mirror of https://github.com/qiurunze123/miaosha
39 lines
1.3 KiB
Java
39 lines
1.3 KiB
Java
package com.geekq.miaosha.redis.redismanager;
|
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URISyntaxException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
public class RedisLimitRateWithLUA {
|
|
|
|
public static boolean accquire() throws IOException, URISyntaxException {
|
|
Jedis jedis = new Jedis("39.107.245.253");
|
|
|
|
String lua =
|
|
"local key = KEYS[1] " +
|
|
" local limit = tonumber(ARGV[1]) " +
|
|
" local current = tonumber(redis.call('get', key) or '0')" +
|
|
" if current + 1 > limit " +
|
|
" then return 0 " +
|
|
" else " +
|
|
" redis.call('INCRBY', key,'1')" +
|
|
" redis.call('expire', key,'2') " +
|
|
" end return 1 ";
|
|
|
|
String key = "ip:" + System.currentTimeMillis() / 1000; // 当前秒
|
|
String limit = "3"; // 最大限制
|
|
List<String> keys = new ArrayList<String>();
|
|
keys.add(key);
|
|
List<String> args = new ArrayList<String>();
|
|
args.add(limit);
|
|
jedis.auth("youxin11");
|
|
String luaScript = jedis.scriptLoad(lua);
|
|
Long result = (Long) jedis.evalsha(luaScript, keys, args);
|
|
return result == 1;
|
|
}
|
|
}
|