I implemented a weak person damping mechanism mechanism in phunction using only APC, here's how I use it:
// allow 60 requests every 30 seconds // each request counts as 1 (expensive operations can use higher values) // keep track of IPs by REMOTE_ADDR (ignore others) $throttle = ph()->Throttle($ttl = 30, $exit = 60, $count = 1, $proxy = false); if ($throttle === true) { // IP exceded 30 requests in the last 60 seconds, die() here } else { // $throttle is a float // number of requests in the last 30 seconds / 30 seconds /* 1 req / 30 = 0,033 sec 5 req / 30 = 0,166 sec 10 req / 30 = 0,333 sec 15 req / 30 = 0,5 sec 20 req / 30 = 0,666 sec 25 req / 30 = 0,833 sec 30 req / 30 = 1 sec */ usleep(intval(floatval($throttle) * 1000000)); }
I use this on my Front-Controller and pass the value to the routing method, but that's a different story.
The bottom line is that if you use APC, you can store things very quickly in memory and with low memory consumption, because APC follows the FILO methodology. If you need a higher timeout, you can use something that is not based on memory.
BTW: MySQL supports tables with the MEMORY engine.
The problem with sleep() :
A typical Apache web server with PHP installed as a module will consume about 10 MB of RAM per instance in order to avoid exceeding your available bar, there are some Apache options that you can configure to limit the maximum number of instances that Apache can start with.
The problem is that you are sleep() , this instance is still active and with enough requests can eventually eat up all available slots for launching new servers, thereby making your website inaccessible until it is completed some pending requests.
There is no way to overcome this from PHP AFAIK, so in the end it is up to you.
The principle is the same for system throttling:
function systemWide($ttl = 86400, $exit = 360) { if (extension_loaded('apc') === true) { $key = array(__FUNCTION__); if (apc_exists(__FUNCTION__) !== true) { apc_store(__FUNCTION__, 0, $ttl); } $result = apc_inc(__FUNCTION__, 1); if ($result < $exit) { return ($result / $ttl); } return true; } return false; }