Sometimes I want to quickly clear the memcache cache when testing or other applications. I will summarize several methods to clear the cache in memcache.
In the past, some colleagues needed me to clear the memcache cache. I always used the kill command to stop this process. Later I learned a simpler method to clear the memcachd cache and record it, for emergency purposes:
1. First, use the ssh command to log on 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 |
After entering the root password, you can log on to the corresponding server;
2. Use the telnet command to connect to the memcached startup port specified in the tomcat service configuration file:
The Code is as follows: |
Copy code |
# Telnet to localhost 11211 |
Shown later:
Trying 127.0.0.1...
Connected to localhost. localdomain (127.0.0.1 ).
Escape character is '^]'.
3. Enter the following content and press enter to clear the cache content:
The Code is as follows: |
Copy code |
Flush_all |
4. exit telnet and run the quit command. Then exit the remote host.
Clear expired Cache
The Code is as follows: |
Copy code |
/** * 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 |
Batch deletion scheme of memcache Cache
By default, memcache only supports delete (key) and flush_all. These two methods are too extreme to meet your specific needs. For example, you can delete all the caches starting with 'aaaaaaaa _ 'in batches, what should I do at this time?
1 getExtendStats traverses all items and deletes the specified key (not recommended)
There are corresponding php code and perl programs on the Internet. If you are interested, you can use them during local testing, but do not use them on real servers.
2 memcache combined with DB
Method: each time you set the cache, the key value is stored in the database. When you want to delete the cache, You can query the database, query the corresponding information, and delete it in memcache.
Disadvantage: Data waste pants Disk
3 memcache pseudo namespace (recommended)
Memcache does not provide a namespace by default, but you can set a global variable to simulate the namespace. The Code is as follows:
The Code is as follows: |
Copy code |
<? Php // Generate a key to save the namespace $ Ns_key = $ memcache-> get ("foo_namespace_key ");
// If the key does not exist, it is created. The current timestamp is used as the identifier by default. If ($ ns_key = false) $ memcache-> set ("foo_namespace_key", time ());
// Generate a real key based on namespace_key to ensure that it is a unique key value $ My_key = "foo _". $ ns_key. $ otherParms; // Use the spliced my_key value to set the data to be cached. $ Memcache-> set ($ my_key, $ value, false, expire );
// Or the key value to obtain the previously stored Cache $ Memcaceh-> get ($ my_key );
// When you need to delete objects in the entire namespace, for example, after changing the database or deleting some information // If you change the ns_key value, you will never access the ns_key in the past when you access the cache, so that you can delete the cache in batches. $ Memcache-> set ("foo_namespace_key", time ()); ?>
|
These are my personal opinions.