Home  >  Article  >  Backend Development  >  Detailed introduction to code examples of PHP's Memcache class (domain operation) that supports group operations

Detailed introduction to code examples of PHP's Memcache class (domain operation) that supports group operations

黄舟
黄舟Original
2017-03-08 10:17:101114browse

Memcache is a cache method commonly used in PHP development and is an essential component in high-concurrency systems.
In actual development, Memcache has an unsatisfactory problem, that is, Memcache cannot support group operations on keys.

Group operations can also be called domain operations. For example, an article system uses Memcache in the front-end part to cache list page data and article detail page data. Both types of data are relatively large. Then, when an article is published in the background, the list page should be updated to the latest list - which may involve many list pages. Of course, for the article detail page, it does not need to be updated.

Okay, at this time we need to delete the original cache so that the program can automatically update the list page data. However, there is a problem with using Memcache's flush function, that is, it will clear all data, including data on list pages and article pages. Under large concurrency conditions, when all caches are deleted and the cache is rebuilt, there will be a very high load. produce.

In addition, there may be situations where some cache variables that you do not want to delete will be lost, such as program configurations, database table structures that are cached to speed up, etc.

So we need a caching mechanism that supports group operations. We can set the list page to one group, the article page data to another group, the program configuration to another group, and so on. When you need to rebuild the list page, you only need to delete all the data in the list page group without affecting the data in other groups.

After testing several solutions, the following solution is the most ideal and fast. Let’s look at the code first, and then talk about the principle:

<?php 
class MyCache 
{ 
    private $mmc = null; 
    private $group = null; 
    function __construct($group){ 
        if(!class_exists(&#39;mmcache&#39;)){ 
            $this->mmc = false; 
            return; 
        } 
        $this->mmc = new memcache(); 
        $this->mmc->addServer(&#39;192.168.1.5&#39;, 11211); 
        $this->mmc->addServer(&#39;192.168.1.6&#39;, 11211); 
        $this->group = $group; 
        $this->version = $this->mmc->get(&#39;version_&#39;.$group); 
	if(empty($this->version)){
	 $this->version=1;	
	}   
    } 
    function set($key, $var, $expire=3600){ 
        if(!$this->mmc)return; 
	$this->check_version($this->group,$this->version,$key);
        return $this->mmc->set($this->group.&#39;_&#39;.$this->version.&#39;_&#39;.$key, $var, $expire); 
    } 
    function get($key){ 
        if(!$this->mmc)return; 
	$this->check_version($this->group,$this->version,$key);
        return $this->mmc->get($this->group.&#39;_&#39;.$this->version.&#39;_&#39;.$key); 
    } 
    function incr($key, $value=1){ 
        if(!$this->mmc)return; 
        return $this->mmc->increment($this->group.&#39;_&#39;.$this->version.&#39;_&#39;.$key, $value); 
    } 
    function decr($key, $value=1){ 
        if(!$this->mmc)return; 
        return $this->mmc->decrement($this->group.&#39;_&#39;.$this->version.&#39;_&#39;.$key, $value); 
    } 
    function delete($key){ 
        if(!$this->mmc)return; 
        return $this->mmc->delete($this->group.&#39;_&#39;.$this->version.&#39;_&#39;.$key); 
    } 
    function flush(){ 
        if(!$this->mmc)return; 
        ++$this->version; 
        $this->mmc->set(&#39;version_&#39;.$this->group, $this->version); 
    } 
   function check_version($goup,$version,$key){
		if($version>1){
			$version_old=$version-1;
			return $this->mmc->delete($goup.&#39;_&#39;.$version_old.&#39;_&#39;.$key);
		}
    }

} 
?>


The above class is relatively complete , including linking to the Memcache service, setting and getting values, increasing and decreasing values, as well as deleting keys and deleting all (flush). This includes regular Memcache operation functions and extensions to full delete (flush) operations.

As you can see from the code, the flush function of the support group is implemented through the version key. That is, every time the variable of the group is saved, the key of the variable will be added with the version value. Version The value is a number (starting from 1), and the version value is used when storing and retrieving the key.

When the developer wants to flush the data of the current group, the flush operation simply changes some version values ​​(plus one). Then, the next time the key is accessed, the original value will not be obtained. ——Because the version has changed, that is, the key name has changed. In this way, the original value will be automatically recycled by Memcache without any efficiency overhead. Moreover, the program only adds the storage and retrieval of a version, the amount of data is extremely small, and it basically has no impact on the system efficiency.

Through the above classes, group operations can be performed on the Memcache cache. This PHP class can also be extended, such as adding a socket to directly access the interface function of memcache, so that there is no need to install the memcache extension in the PHP environment. Class, which is more effective in avoiding misoperations of flush, and after adding caching mechanisms such as apc, socket access to the memcache interface will not be much slower than expansion.

In addition, the MyCache class has an additional function: when the memcache service fails, the MyCache class simply returns a null value without directly making an error.
The following is attached with the usage method of MyCache class:

// 引入定义 
include(&#39;MyCache.php&#39;); 
 
// 实例化 
$mc = new MyCache(&#39;abc&#39;); // 要有域 
 
// 设置值 
$mc->set(&#39;word&#39;, &#39;hello world&#39;, 900); 
 
// 取得值 
echo $mc->get(&#39;word&#39;); 
 
// 删除值 
$mc->delete(&#39;word&#39;); 
echo $mc->get(&#39;word&#39;); 
 
$mc->set(&#39;counter&#39;, 1, 290000); 
echo $mc->get(&#39;counter&#39;); 
 
// 增加值 
$mc->incr(&#39;counter&#39;); 
$mc->incr(&#39;counter&#39;); 
echo $mc->get(&#39;counter&#39;); 
 
// 减少值 
$mc->decr(&#39;counter&#39;); 
echo $mc->get(&#39;counter&#39;); 
 
// 按组删 
$mc->flush();

               

The above is a detailed introduction to the code examples of the Memcache class (domain operation) that supports group operations in PHP, and more For related content, please pay attention to the PHP Chinese website (www.php.cn)!

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