This article to share a PHP clear memcache outdated cache program code, memcached will use the LRU algorithm to remove cached content or use the deletion of expired content, and sometimes memcached such a mechanism to free memory can not meet all applications, Therefore, we have realized the function of the unified deletion of expired content on the basis of PHP, which is suitable for the regular cleaning, and the friends who need to understand the study can be consulted.
| The code is as follows |
Copy Code |
|
/**
* Memcached Expired Memory Recycle
*/
Class Mem_dtor extends Memcache
{
Private $server _id;
Public function __construct ($host, $port)
{
$this->server_id = "$host: $port";
$this->connect ($host, $port);
}
Reclaim All expired memory
Public Function GC ()
{
$t = time ();
$_this = $this;
$func = function ($key, $info) use ($t, $_this)
{
if ($info [1]-$t Delete ($key);
}
};
$this->lists ($func);
}
View information for all cached content
Public Function info ()
{
$t = time ();
$func = function ($key, $info) use ($t)
{
echo $key, ' => Exp: ', $info [1]-$t, "n"; To view the remaining expiration time of a cached object
};
$this->lists ($func);
}
Private function lists ($func)
{
$sid = $this->server_id;
$items = $this->getextendedstats (' items '); Get memcached Status
foreach ($items [$sid] [' Items '] as $slab _id => $slab)//Get all slab of the specified server ID
{
$item = $this->getextendedstats (' Cachedump ', $slab _id,0); Traverse all Slab
foreach ($item [$sid] as $key => $info)//Get cached object information in slab
{
$func ($key, $info);
}
}
}
}
$mem = new Mem_dtor (' 127.0.0.1 ', 11211);
$mem->info ()//View status
$mem->GC (); Recovery
|