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 of all, if we have an article page need to do pagination display, the type of paging by the classification of pagination, by the latest pagination, by hot spots paging, customized way to page, 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. Then I ask you, how many kinds of paging type, each type of how many pages, key composition is what each, if the page with other get query parameters, how do you know what the get all passed the value, do not know these, 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, so that all paging related cache is invalidated.
//Note: If the following functions are already initialized $memcachedclassArticle {Private $article _version= ' Article_version '; Public functionGetArticle ($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 data back from the database and set up a new memcached cache } return $artdata; } Public functionUpdatearticle ($conditions,$data){ //Update database Data Operations//update 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){ $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 add, modify, delete the article, the call _ Setarticleversion () function, 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 cached data, from the database to find, and then generate a new version of the cache, The old cache automatically frees up memory space after the time expires.
PHP use memcached to do a real time page