This article will share with you a PHP code for clearing Memcache expired cache programs. Memcached will use the LRU algorithm to delete cached content or delete expired content during use, sometimes, the memory release mechanism such as Memcached cannot satisfy all applications. Therefore, we have implemented the function of uniformly deleting expired content on the basis of PHP, which is suitable for regular cleaning, for more information, see. This article will share with you a PHP code for clearing Memcache expired cache programs. Memcached will use the LRU algorithm to delete cached content or delete expired content during use, sometimes, the memory release mechanism such as Memcached cannot satisfy all applications. Therefore, we have implemented the function of uniformly deleting expired content on the basis of PHP, which is suitable for regular cleaning, for more information, see.
Script ec (2); script
This article will share with you a PHP code for clearing Memcache expired cache programs. Memcached will use the LRU algorithm to delete cached content or delete expired content during use, sometimes, the memory release mechanism such as Memcached cannot satisfy all applications. Therefore, we have implemented the function of uniformly deleting expired content on the basis of PHP, which is suitable for regular cleaning, for more information, see.
| The Code is as follows: |
|
|
/**
* Memcached expired memory recovery
*/
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 about All cached content
Public function info ()
{
$ T = time ();
$ Func = function ($ key, $ info) use ($ t)
{
Echo $ key, '=> Exp:', $ info [1]-$ t, "n"; // view the remaining expiration time of the cache object
};
$ This-> lists ($ func );
}
Private function lists ($ func)
{
$ Sid = $ this-> server_id;
$ Items = $ this-> getExtendedStats ('items '); // get the memcached status
Foreach ($ items [$ sid] ['items '] as $ slab_id => $ slab) // obtain all slabs of the specified server id
{
$ Item = $ this-> getExtendedStats ('cachedump ', $ slab_id, 0); // traverse all Slab
Foreach ($ item [$ sid] as $ key => $ info) // obtain the cached object information in Slab
{
$ Func ($ key, $ info );
}
}
}
}
$ Mem = new mem_dtor ('2017. 0.0.1 ', 127 );
$ Mem-> info (); // view the status
$ Mem-> gc (); // Recycle |