On the topic of caching

Source: Internet
Author: User
Current status   There is a project where you can get data from the cache directly using the Yii::app ()->memcache->get method. At first glance, there seems to be nothing wrong. But as the project expands, processes become more complex, and the cache content increases, the hidden dangers of this approach become more apparent. Every revision or data change requires looking for a large piece of code to modify (and possibly an implicit assembly call). Even if you miss a place that has not been corrected, it will cause bugs in the system, and not even a source unknown error.   Face the above problems, I say my improvement plan. The following improvements are only representative of my personal opinion, if there is a better way, welcome to discuss together.     Optimization   There is an old saying in the computer world: as long as there is an extra layer of indirection, there is no problem in computer science (referenced from page 19th of the Object-c Basic tutorial). My approach is to add a layer to prevent low-level and collaborative developers from directly contacting the data. To avoid the confusion caused by excessive caching, we need to encapsulate the cache. Take the online store as an example:   [PHP]   class eshopcache{      Private $_cachekey = Array (' UserName ', ' shoptitle ', ' is Valpro ', ' isvalshop ');       Private $_cache = Array ();      Public Function __get ($key) {      if (array_in ($key, $this->_cachekey)) {          if (!array_key_exixts ($key, $this->_cache)) {            & nbsp $this->_cache[$key] = Yii::app ()->memcache->get ($key);           {          Return $this->_cache[$key];  }   return false;  }  }     This is a much simpler and more semantic way to use the method. [PHP]   $eshopCache = new Eshopcache;   Echo $ eshopcache->username;   if ($ eshopcache->isvalshop = = 1) {      echo ' shop has been authenticated by entity ';  }     If there is a need for change Data costs to modify, we only need to modify this class, do not need to the next layer of each interface to modify, do not worry about forgetting to modify the lower layers of encapsulation and covert calls.     Continuous Improvement   There is another problem, if demand changes again. Shop to display is the user's custom nickname (nickname), and is no longer the user name (UserName), can we only go over the file to modify it? The improvement scheme is as follows:   [PHP]   class eshopcache{      Private $_cachekey = Array (' UserName ', ' shoptitle ', ' Isva Lpro ', ' isvalshop ');       Private $_aliaeses = Array (' UserName ' => ' nickname ');        Private $_cache = Array ();      Public Function __get ($key) {      if (array_in ($key, $this->_cachekey)) {          if (!array_key_exixts ($key, $this->_cache)) {            & nbsp; $_key = Array_key_exists ($key, $this->_aliases)? $this->_aliases[$key]: $key;               $this->_cache[$key] = Yii::app ()->memcache->get ($_key);           {          return $this->_cache[$key];  }   return false;  }  }     to this, the function of this class already can satisfy us to use. The solution I provide is to use the "Agent mode" in design mode to indirectly access the cached data. If you have any objections, you are welcome to send emails to yagas@sina.com to discuss together.  

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.