Memcache is a frequently used caching method in PHP Development. it is an essential component of a high-concurrency system. In actual development, Memcache does not support key group operations. Group operations can also be called domain operations. for example, an article system uses Memcache on the front-end to cache the list page data and detailed page data, the two Memcache methods are commonly used in PHP Development. they are an essential part of high-concurrency systems.
In actual development, Memcache does not support key group operations.
Group operations can also be called domain operations. for example, in an article system, Memcache is used on the front-end to cache the list page data and detailed page data. both of the two types of data have a large amount of data. 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 detailed page of the article, it does not need to be updated.
Okay. in this case, we need to delete the original cache so that the program can automatically update the list page data. However, the flush function of Memcache has a problem, that is, it clears all the data, including the data on the list page and article page. under the condition of high concurrency, when deleting all the caches and recreating the cache, a very high load will be generated.
In addition, some cache variables that you do not want to delete may also be lost, such as program configuration and the table structure that the database stores in the cache to speed up.
Therefore, we need a caching mechanism that supports group operations. we can set the list page to a group, the number of document pages is another group, and the program configuration is another group. To recreate the list page, you only need to delete all the data in the group on the list page without affecting the data in other groups.
I tested several solutions, but the following solutions are the most ideal and fast. let's look at the code first and talk about the principle:
mmc = false; return; } $this->mmc = new memcache(); $this->mmc->addServer('192.168.1.5', 11211); $this->mmc->addServer('192.168.1.6', 11211); $this->group = $group; $this->version = $this->mmc->get('version_'.$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.'_'.$this->version.'_'.$key, $var, $expire); } function get($key){ if(!$this->mmc)return; $this->check_version($this->group,$this->version,$key); return $this->mmc->get($this->group.'_'.$this->version.'_'.$key); } function incr($key, $value=1){ if(!$this->mmc)return; return $this->mmc->increment($this->group.'_'.$this->version.'_'.$key, $value); } function decr($key, $value=1){ if(!$this->mmc)return; return $this->mmc->decrement($this->group.'_'.$this->version.'_'.$key, $value); } function delete($key){ if(!$this->mmc)return; return $this->mmc->delete($this->group.'_'.$this->version.'_'.$key); } function flush(){ if(!$this->mmc)return; ++$this->version; $this->mmc->set('version_'.$this->group, $this->version); } function check_version($goup,$version,$key){if($version>1){$version_old=$version-1;return $this->mmc->delete($goup.'_'.$version_old.'_'.$key);} }} ?>
The above classes are complete, including links to the Memcache service, setting and obtaining values, adding or decreasing values, deleting keys, and fully deleting (flush ). This includes the general Memcache operation function and the extension of the flush operation.
From the code, we can see that the key version is used to implement the flush function of a group, that is, every time the group's variables are stored, the version value is added to the key of the variable. the version value is a number (starting from 1). when the key is saved and retrieved, the version value is used.
When the developer wants to flush the data of the current group, the flush operation simply changes the value of some versions (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. In addition, the program only adds a version for storage and retrieval. the data size is extremely small, which has no impact on system efficiency.
Through the above classes, you can perform group operations on the Memcache cache, and this PHP class can continue to be extended. for example, you can add a socket to directly access the memcache interface function, in this way, the memcache extension class does not need to be installed in the PHP environment. this effectively avoids the misoperation of flush, and after the cache mechanism such as apc is added, socket access to the memcache interface is not 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 describes how to use the MyCache class:
// Introduce the definition include ('mycache. php '); // instantiate $ mc = new MyCache ('ABC'); // you must have a domain. // set the value $ mc-> set ('word ', 'Hello World', 900); // obtain the value echo $ mc-> get ('word'); // delete the value $ mc-> delete ('word '); echo $ mc-> get ('word'); $ mc-> set ('counter', 1, 290000); echo $ mc-> get ('counter '); // added $ mc-> incr ('counter'); echo $ mc-> get ('counter '); // reduce the value by $ mc-> decr ('counter'); echo $ mc-> get ('counter'); // delete $ mc-> flush () by Group ();
The above section details the content of the code instance of the Memcache class (domain operation) that PHP supports group operations. For more information, see PHP Chinese network (www.php1.cn )!