Home >Backend Development >PHP Tutorial >Summary of some methods to clear the cache in memcache_PHP tutorial
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 | ||||
|
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 |
:
Trying 127.0.0.1…
Connected to localhost.localdomain (127.0.0.1).
代码如下 | 复制代码 |
flush_all |
The code is as follows | Copy code |
flush_all
|
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:
The code is as follows
|
Copy code
|
||||
//Generate a key to save namespace |
//If key does not exist, create it and use the current timestamp as the identifier by default
//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