Here is the function that I use here at DaniWeb to manage flood control. It keeps track of how often a specific user or IP address is making a request, and increments a counter. If there have been no requests after 5 minutes, the counter resets to 0. It returns false if the counter reaches more than 10 requests (in that 5 minute period).
Here is what the Codeigniter redis library looks like:
/**
* Get cache metadata
*
* @param string $key Cache key
* @return array
*/
public function get_metadata($key)
{
$value = $this->get($key);
if ($value !== FALSE)
{
return array(
'expire' => time() + $this->_redis->ttl($key),
'data' => $value
);
}
return FALSE;
}
/**
* Save cache
*
* @param string $id Cache ID
* @param mixed $data Data to save
* @param int $ttl Time to live in seconds
* @param bool $raw Whether to store the raw value (unused)
* @return bool TRUE on success, FALSE on failure
*/
public function save($id, $data, $ttl = 60, $raw = FALSE) { ... }
/**
* Increment a raw value
*
* @param string $id Cache ID
* @param int $offset Step/value to add
* @return mixed New value on success or FALSE on failure
*/
public function increment($id, $offset = 1) { ... }