PHP clears Memcache expired cache program code-PHP source code

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.