PHP clears memcache expired cache program code

Source: Internet
Author: User

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

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.