using memcached to do paged cache, many people may find it troublesome. Because in the process of adding, modifying, deleting, you do not know what data will affect, and if all the paging related data cache is deleted and rebuilt again, the implementation is cumbersome, even not feasible, so simply to use MySQL directly paging, simple and convenient, but this performance is also reduced. This chapter is about a simple implementation of using memcached to do paged cache method.
First, if we have an article page that needs to be paginated, the paging type is paginated by category, paged by the latest, paged by hotspots, paginated by custom, and so on. This is a tricky issue, and our updates to the data affect which pages we are not aware of and do not know which related caches need to be deleted. You might think that it would be nice to delete all types of paged cache when updating data. So I ask you, how many types of paging are there, how many pages each type has, key What values are passed and how do you delete all the paged cache?
In this case, you may feel a little disappointed, don't you think it would be so troublesome to use memcached to do paged cache. So, is there a simple solution? The answer is yes, please believe that the purpose of my writing this article is to tell you a simple solution. To say so much, in fact, we need to solve the core problem is only one, we add, modify, delete the article data, can make the paged cache is invalidated. The solution is also simple, we just need to introduce the version number, in all the affected memcached key to Add version number, when we add, modify, delete the article data, version number +1, This would mean that all paging-related caches are invalidated.
The implementation code is as follows:
<?php //Note: If the following functions have already been initialized $memcached class Article { private $article _version = ' article_version '; public function getarticle ($type = ' new ', $page = ' 1 ', $limit =0) { //set memcached key, add version number at the end of key $cache _id = ' Art_type '. $type. ' _page '. $page. ' _limit '. $limit. ' V_ '. $this->_getarticleversion (); //Get paged data $artdata = $memcached->get ($cache _id); if ( FALSE === $artdata) { // Get the data back from the database and set up a new memcached cache } return $artdata; } public function updatearticle ($conditions, $data) { //Updating database Data operations //updates the article version so that all article table-related caches are invalidated and new cache data is generated the next time the Getarticle function is called $this->_setarticleversion (); } private function _getarticleversion () { $article _version_num = $memcached->get ($ this->article_version); if ( FALSE === $ Article_version_num) {&NBSP;&Nbsp; $article _version_num = 1; $memcached->set ($this article_version, $article _version_num, 86400); } return $article _version_num; } private function _ Setarticleversion () { $article _version_num = $ Memcached->get ($this->article_version); $article _ version_num++; $memcached->set ($this->article_ version, $article _version_num, 86400); } } ?>
isn't it simple? that is, more than the usual memcached cache two functions _ Getarticleversion () and _ Setarticleversion (), so that when we have added, modified, When deleting an article, call the _ Setarticleversion () function, so that the version number +1, that is, the previous version of the data is invalid, because the article paged cache data when the key has joined _ Getarticleversion (), so the new version number of the cache data, from the database to find, and then generate a new version of the cache, the old cache will automatically free up memory space after the time expires.
Use memcached to do a real time page cache