Home >Backend Development >PHP Tutorial >Summary of some methods to clear the cache in memcache_PHP tutorial

Summary of some methods to clear the cache in memcache_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:07:471592browse

Sometimes I want to clear the memcache cache quickly when testing or other applications. Below I have summarized some methods to clear the cache in memcache. I hope the method will be useful to everyone.

Some colleagues have needed me in the past. To clear the memcache cache, I always use the kill command to end the process. Later I learned that there is an easier way to clear the memcachd cache and record it in case of emergency:

1. First use the ssh command to log in to the server where memcached is located. The command is as follows:

The code is as follows Copy code
 代码如下 复制代码

#ssh root@192.168.1.1

#ssh root@192.168.1.1

Enter the root password to log in to the corresponding server;

2. Use the telnet command followed by the memcached startup port specified in the tomcat service configuration file:
 代码如下 复制代码

#telnet localhost 11211

The code is as follows Copy code

#telnet localhost 11211

Display after

:

Trying 127.0.0.1…
Connected to localhost.localdomain (127.0.0.1).

Escape character is ‘^]’.

 代码如下 复制代码

flush_all

3. At this time, enter the following content and press Enter to clear the cache content:

The code is as follows Copy code
flush_all

4. Finally exit telnet and use the quit command, then exit to exit the remote host.

Clear expired cache

 代码如下 复制代码
/**
* Memcached的过期内存回收
*/
class mem_dtor extends Memcache
{
private $server_id;
public function __construct($host,$port)
{
$this->server_id = "$host:$port";
$this->connect($host,$port);
}
// 回收所有过期的内存
public function gc()
{
$t = time();
$_this = $this;
$func = function($key,$info) use ($t,$_this)
{
if($info[1] - $t delete($key);
}
};
$this->lists($func);
}
// 查看所有缓存内容的信息
public function info()
{
$t = time();
$func = function($key,$info) use ($t)
{
echo $key,' => Exp:',$info[1] - $t,"n"; //查看缓存对象的剩余过期时间
};
$this->lists($func);
}
private function lists($func)
{
$sid = $this->server_id;
$items = $this->getExtendedStats('items'); //获取memcached状态
foreach($items[$sid]['items'] as $slab_id => $slab) //获取指定server id 的 所有Slab
{
$item = $this->getExtendedStats('cachedump',$slab_id,0); //遍历所有Slab
foreach($item[$sid] as $key => $info) //获取Slab中缓存对象信息
{
$func($key,$info);
}
}
}
}
$mem = new mem_dtor('127.0.0.1',11211);
$mem->info();//查看状态
$mem->gc(); //回收
The code is as follows Copy code
/** * Memcached expired memory recycling */ class mem_dtor extends Memcache { private $server_id; public function __construct($host,$port) { $this->server_id = "$host:$port"; $this->connect($host,$port); } //Recycle 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 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(); //Recycle

Batch deletion solution for memcache cache

Memcache only supports delete(key) and flush_all by default. These two methods are too extreme and cannot meet the specific needs of users. For example, batch deletion of all caches starting with 'aaaaaaaa_', what should I do at this time?
1 getExtendStats traverses all items and deletes the specified key (not recommended)
There are corresponding php codes and perl programs on the Internet. If you are interested, you can take a look. You can use them during local testing, but please do not use them on a real server.

2 memcache combined with DB


Method: Each time the cache is set, the key value is stored in the database. When the cache is to be deleted, the database is queried to find the corresponding information and deleted in memcache
Disadvantages: Waste of data and disk
3 memcache pseudo namespace (recommended)
Memcache does not provide a namespace by default, but you can set a global variable to simulate a namespace. The code is as follows:

//Generate a key to save namespace
The code is as follows
 代码如下 复制代码

//生成一个用来保存 namespace 的 key
$ns_key = $memcache->get("foo_namespace_key");  
  
//如果 key 不存在,则创建,默认使用当前的时间戳作为标识
if($ns_key===false) $memcache->set("foo_namespace_key",time());  
  
//根据 namespace_key 生成真正的 key,确保是唯一的key值  
$my_key = "foo_".$ns_key.$otherParms; 


//然后利用拼接的my_key值设置你需要缓存的各种数据
$memcache->set($my_key,$value,false,expire); 


//或者key值获得以前存储的缓存
$memcaceh->get($my_key);
  
  
//需要删除整个 namespace 里的对象的时候,如:更改数据库或者删除某些信息后
//将ns_key的值改变,则以后在访问缓存时,以前时间的将永远不会别访问到,以此来实现批量删除缓存 
 $memcache->set("foo_namespace_key",time());
?>  

Copy code

$ns_key = $memcache->get("foo_namespace_key");

//If key does not exist, create it and use the current timestamp as the identifier by default

if($ns_key===false) $memcache->set("foo_namespace_key",time()); //Generate the real key based on namespace_key to ensure it is the only key value $my_key = "foo_".$ns_key.$otherParms; //Then use the spliced ​​my_key value to set the various data you need to cache $memcache->set($my_key,$value,false,expire); //Or the key value gets the previously stored cache $memcaceh->get($my_key);
//When you need to delete the objects in the entire namespace, such as after changing the database or deleting some information //Change the value of ns_key, then when accessing the cache in the future, the previous time will never be accessed, so as to achieve batch deletion of the cache
$memcache->set("foo_namespace_key",time());
?> The above is my personal opinion, please give me your opinion http://www.bkjia.com/PHPjc/629897.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629897.htmlTechArticleSometimes I want to clear the memcache cache quickly when testing or other applications. Below I have summarized several clearing methods. Some methods of caching in memcache, I hope the methods will be useful to everyone...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn