/** * Rate Limiting * http://stackoverflow.com/a/668327/670662 * @param string $id * @return boolean */ public function check($id, $use = 1.0) { $rate = $this->maxRequests / $this->period; $t_key = $this->keyTime($id); $a_key = $this->keyAllow($id); if ($this->adapter->exists($t_key)) { $c_time = time(); $time_passed = $c_time - $this->adapter->get($t_key); $this->adapter->set($t_key, $c_time, $this->ttl); $allow = $this->adapter->get($a_key); $allow += $time_passed * $rate; if ($allow > $this->maxRequests) { $allow = $this->maxRequests; } if ($allow < 1.0) { $this->adapter->set($a_key, $allow, $this->ttl); return 0; } else { $allow -= $use; $this->adapter->set($a_key, $allow, $this->ttl); return (int) ceil($allow); } } else { $allow = $this->maxRequests - $use; $this->adapter->set($t_key, time(), $this->ttl); $this->adapter->set($a_key, $allow, $this->ttl); return (int) ceil($allow); } }
public function set($key, $value, $ttl = null, array $options = []) : bool { $key = array_key_exists('noprefix', $options) ? $key : $this->prefix . $key; return $this->driver->set($key, $value, $ttl, $options); }