1,使用Redis的list数据结构类型实现限流
代码如下:
String key = "userId" + "接口名" ;
int listLength = llen(key);
if (listLength < 10) {
lpush(key, new ());
} else {
long time = lindex(key, -1);
if (now() - time < 60) {
System.out.println("访问频率超过了限制,请稍后再试");
} else {
lpush(key, now);
ltrim(key, 0, 9);
}
}
list里面只需要存时间戳即可。