| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 
 | function isActionAllowed($userId, $action, $period, $maxCount){
 $redis = new Redis();
 $redis->connect('127.0.0.1', 6379);
 $key = sprintf('hist:%s:%s', $userId, $action);
 $now = msectime();
 
 $pipe=$redis->multi(Redis::PIPELINE);
 $pipe->zadd($key, $now, $now);
 $pipe->zremrangebyscore($key, 0, $now - $period * 1000);
 $pipe->zcard($key);
 $pipe->expire($key, $period  + 1);
 $replies = $pipe->exec();
 return $replies[2] <= $maxCount;
 }
 
 
 function msectime() {
 list($msec, $sec) = explode(' ', microtime());
 $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
 return $msectime;
 }
 
 
 for ($i=0; $i<20; $i++){
 var_dump(isActionAllowed("110", "reply", 60, 5));
 }
 
 
 
 |