the approximate code is as follows:
func(id, ip){
int countId = jedis.hget("count", id);
int countIP = jedis.get("count", ip);
if(countId >= 5 || countIP>=5){
return;
}
... //
//
int countId = jedis.hget("count", id);
int countIP = jedis.get("count", ip);
if(countId >= 5 || countIP>=5){
doSomething();
jedis.hincrBy("count", id, 1);
jedis.hincrBy("count", ip, 1);
}
}
the above code only expresses the general meaning, because it is obvious that there will be problems in the case of concurrency
my idea is, for example, to do a, casIncrBy ("count" like CAS, id, 1, 5), this atomic operation increases the key value by 1, and fails if it is > 5. Then judge whether to fail or not. If you fail, you will not doSomething (),. If you succeed, you will doSomething ().
.how can it be achieved?