Memcache class that supports Group operations in PHP

Source: Internet
Author: User
Tags apc

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, 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:
 
 
<? Php
Class MyCache
{
Private $ mmc = null;
Private $ group = null;
Private $ version = 1;
Function _ construct ($ group ){
If (! Class_exists ('mcac ')){
$ This-> mmc = false;
Return;
}
$ This-> mmc = new memcache ();
$ This-> mmc-> addServer ('192. 168.1.5 ', 192 );
$ This-> mmc-> addServer ('192. 168.1.6 ', 192 );
$ This-> group = $ group;
$ This-> version = $ this-> mmc-> get ('version _ '. $ group );
}
Function set ($ key, $ var, $ expire = 3600 ){
If (! $ This-> mmc) return;
Return $ this-> mmc-> set ($ this-> group. '_'. $ this-> version. '_'. $ key, $ var, $ expire );
}
Function get ($ key ){
If (! $ This-> mmc) return;
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 );
}
}
?>
 
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 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 value
$ Mc-> incr ('counter ');
$ Mc-> incr ('counter ');
Echo $ mc-> get ('counter'); Memcache is a frequently used caching method in PHP development. It is an essential component in 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:
 
 
<? Php
Class MyCache
{
Private $ mmc = null;
Private $ group = null;
Private $ version = 1;
Function _ construct ($ group ){
If (! Class_exists ('mcac ')){
$ This-> mmc = false;
Return;
}
$ This-> mmc = new memcache ();
$ This-> mmc-> addServer ('192. 168.1.5 ', 192 );
$ This-> mmc-> addServer ('192. 168.1.6 ', 192 );
$ This-> group = $ group;
$ This-> version = $ this-> mmc-> get ('version _ '. $ group );
}
Function set ($ key, $ var, $ expire = 3600 ){
If (! $ This-> mmc) return;
Return $ this-> mmc-> set ($ this-> group. '_'. $ this-> version. '_'. $ key, $ var, $ expire );
}
Function get ($ key ){
If (! $ This-> mmc) return;
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 );
}
}
?>
 
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 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 value
$ Mc-> incr ('counter ');
$ Mc-> incr ('counter ');
Echo $ mc-> get ('counter ');
 
// Reduce the value
$ Mc-> decr ('counter ');
Echo $ mc-> get ('counter ');
 
// Delete by group
$ Mc-> flush ();
This article is from the "Zhenzhong technical notebook" blog
 
// Reduce the value
$ Mc-> decr ('counter ');
Echo $ mc-> get ('counter ');
 
// Delete by group
$ Mc-> flush ();
Author: "Zhenzhong technical notebook"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.