Memcache is a more commonly used method of caching in PHP development, and is an essential component in high concurrency systems.
In the actual development, memcache there is a more unsatisfactory problem, that is, memcache can not support the group operation of Key.
Group operations, also known as domain operations, such as an article system, in the foreground section using Memcache cache List page data, article detail page data, the amount of both data is more. Then, when a post is published in the background, the list page should be updated to the latest list--many list pages may be involved. Of course, for the article detail page, it is not necessary to update.
Well, at this point we need to delete the original cache so that the program can automatically update the list page data. But the problem with the flush function of memcache is that it empties all of the data, including the data from the list page and the article page, with very high load when the cache is rebuilt after all caches are deleted under large concurrency conditions.
In addition, there will be some things you do not want to delete the cache variables, will also be lost, such as the configuration of the program, the database to speed up and save to the table structure of the cache.
So we need a support group operation caching mechanism, we can set the list page as a group, the article page data is another group, the program configuration is another group and so on. When you need to rebuild a list page, you only need to delete all the data in the group, without affecting the data in the other group.
Test several scenarios, or the following scenarios are ideal and high speed, we first look at the code, and then the principle:
Class Mycache
{
Private $MMC = null;
Private $group = null;
Private $version = 1;
function __construct ($group) {
if (!class_exists (' MMCache ')) {
$this->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);
}
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 more complete, including links to Memcache services, setting and getting values, adding or subtracting values, and deleting keys and deleting them all (flush). This includes the general Memcache operation function, and an extension to the full delete (flush) operation.
As can be seen from the code, the implementation of the flush function of the support group is implemented by the key of version, that is, each time the variable of the group is saved, the variable key will be added to the version value, and the version value is a number (starting from 1), when saving and fetching key, The version value will be used.
When the developer wants to flush the data for the current group, the flush operation simply changes the value of some version (plus one), then the next time the key is accessed, it will not get the original value-because version changed, that is, the key name has changed. This way the original value is automatically reclaimed by the memcache, without any efficiency overhead. And the program just adds a version of the storage and fetching, the amount of data is very small, the system efficiency basically has no impact.
Through the above class, the Memcache cache can be used for group operations, and this PHP class, you can continue to expand, such as join the socket directly access Memcache interface functions, so that the PHP environment does not need to install Memcache extension class, This is more effective to avoid flush error operation, and after joining the buffer mechanism such as APC, the socket Access Memcache interface is not much slower than the extension.
In addition, the Mycache class has an additional function: When the Memcache service fails, the Mycache class simply returns a null value without a direct error.
Here's how to use the Mycache class:
Introducing Definitions
Include (' mycache.php ');
Instantiation of
$MC = new Mycache (' abc '); To have a domain
Setting the value
$MC->set (' word ', ' Hello World ', 900);
Get value
echo $mc->get (' word ');
Delete value
$MC->delete (' word ');
echo $mc->get (' word ');
$MC->set (' counter ', 1, 290000);
echo $MC->get (' counter ');
Add Value
$MC->incr (' counter ');
$MC->incr (' counter ');
echo $MC->get (' counter '); Memcache is a more commonly used method of caching in PHP development, and is an essential component in high concurrency systems.
In the actual development, memcache there is a more unsatisfactory problem, that is, memcache can not support the group operation of Key.
Group operations, also known as domain operations, such as an article system, in the foreground section using Memcache cache List page data, article detail page data, the amount of both data is more. Then, when a post is published in the background, the list page should be updated to the latest list--many list pages may be involved. Of course, for the article detail page, it is not necessary to update.
Well, at this point we need to delete the original cache so that the program can automatically update the list page data. But the problem with the flush function of memcache is that it empties all of the data, including the data from the list page and the article page, with very high load when the cache is rebuilt after all caches are deleted under large concurrency conditions.
In addition, there will be some things you do not want to delete the cache variables, will also be lost, such as the configuration of the program, the database to speed up and save to the table structure of the cache.
So we need a support group operation caching mechanism, we can set the list page as a group, the article page data is another group, the program configuration is another group and so on. When you need to rebuild a list page, you only need to delete all the data in the group, without affecting the data in the other group.
Test several scenarios, or the following scenarios are ideal and high speed, we first look at the code, and then the principle:
Class Mycache
{
Private $MMC = null;
Private $group = null;
Private $version = 1;
function __construct ($group) {
if (!class_exists (' MMCache ')) {
$this->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);
}
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 more complete, including links to Memcache services, setting and getting values, adding or subtracting values, and deleting keys and deleting them all (flush). This includes the general Memcache operation function, and an extension to the full delete (flush) operation.
As can be seen from the code, the implementation of the flush function of the support group is implemented by the key of version, that is, each time the variable of the group is saved, the variable key will be added to the version value, and the version value is a number (starting from 1), when saving and fetching key, The version value will be used.
When the developer wants to flush the data for the current group, the flush operation simply changes the value of some version (plus one), then the next time the key is accessed, it will not get the original value-because version changed, that is, the key name has changed. This way the original value is automatically reclaimed by the memcache, without any efficiency overhead. And the program just adds a version of the storage and fetching, the amount of data is very small, the system efficiency basically has no impact.
Through the above class, the Memcache cache can be used for group operations, and this PHP class, you can continue to expand, such as join the socket directly access Memcache interface functions, so that the PHP environment does not need to install Memcache extension class, This is more effective to avoid flush error operation, and after joining the buffer mechanism such as APC, the socket Access Memcache interface is not much slower than the extension.
In addition, the Mycache class has an additional function: When the Memcache service fails, the Mycache class simply returns a null value without a direct error.
Here's how to use the Mycache class:
Introducing Definitions
Include (' mycache.php ');
Instantiation of
$MC = new Mycache (' abc '); To have a domain
Setting the value
$MC->set (' word ', ' Hello World ', 900);
Get value
echo $mc->get (' word ');
Delete value
$MC->delete (' word ');
echo $mc->get (' word ');
$MC->set (' counter ', 1, 290000);
echo $MC->get (' counter ');
Add Value
$MC->incr (' counter ');
$MC->incr (' counter ');
echo $MC->get (' counter ');
Decrease value
$MC->decr (' counter ');
echo $MC->get (' counter ');
Delete by group
$MC->flush ();
This article is from the "technical Notepad in the vibration" blog
Decrease value
$MC->decr (' counter ');
echo $MC->get (' counter ');
Delete by group
$MC->flush ();
Author "Technical Notepad in the vibration"
http://www.bkjia.com/PHPjc/478661.html www.bkjia.com true http://www.bkjia.com/PHPjc/478661.html techarticle Memcache is a more commonly used method of caching in PHP development, and is an essential component in high concurrency systems. In the actual development, memcache there is a more unsatisfactory problem, ...