Php Cache technology details and php Cache Implementation code

Source: Internet
Author: User
Tags delete cache
Some information remains unchanged for example, but the information that can be changed is stored in the cache to speed up display. this is very valuable, the common understanding is that some shared information stored on the server is generated on the server. when saving the cache, we can specify that some information will remain unchanged during the next update, however, it is very valuable to store variable information in the cache to accelerate the display speed. The so-called cache is commonly used to store shared information on the server. it is generated on the same server. when saving the cache, we can specify the next update time. for example, we need to update the cache once every 5 minutes.

Data cache: the data cache here refers to the PHP Cache mechanism for database queries. each time you access the page, it first checks whether the cache data exists. if it does not exist, it connects to the database, obtain the data and serialize the query results and save them to the file. the same query results will be obtained directly from the cache table or file.

The most widely used example is the Discuz search function. The result ID is cached in a table and the table is searched in the cache next time when the same keywords are searched.

For example, when multiple tables are joined, an array generated in the appendix is saved to a field in the master table. if necessary, the array is decomposed, the advantage is read-only tables. The disadvantage is that two data synchronization steps are many more steps, and the database is always the bottleneck. Changing the disk speed is the key point.

Page cache:

Each time you access the page, the system first checks whether the corresponding cached page file exists. if it does not exist, it connects to the database to obtain data, displays the page, and generates cache page files at the same time, in this way, the page file will play a role in the next visit. (The template engine and some common PHP Cache mechanism classes on the Internet usually have this function)

Time-triggered cache:

Check whether the file exists and the timestamp is earlier than the set expiration time. if the modified timestamp of the file is greater than the current timestamp minus the expiration timestamp, use the cache; otherwise, update the cache.

Content trigger cache:

When data is inserted or updated, the PHP Cache is forcibly updated.

Static cache:

The static cache mentioned here refers to static, which directly generates HTML, XML, and other text files. it is re-generated once when there is an update. this is suitable for pages that do not change much.

The above content is a code-level solution. I am not too lazy to change the content of the CP framework. the content is similar and easy to implement. it can be used together in several ways, however, the following content is a server-side cache solution. non-code-level solutions can only be achieved through multi-party cooperation.

Memory cache:

Memcached is a high-performance, distributed memory object PHP Cache mechanism system that reduces database loads in Dynamic Applications and improves access speed.

Php buffer:

There are eaccelerator, apc, phpa, and xcache. you don't need to talk about this. you can search for a bunch of them and read them by yourself. if you know what it is, OK.

MYSQL cache:

This is not code-level. the classic database uses this method. See the following running time, such as 0.09xxx.
My post part is modified according to the guy in blue. ini. the MYISAM table of 2 GB can be around 5s. it is said that he has changed it for almost a year.

Reverse proxy-based Web cache:

For example, Nginx, SQUID, mod_proxy (apache2 and above are also divided into mod_proxy and mod_cache)
NGINX example

Use google to find some php Cache technical methods

A php Cache Implementation is developed to implement apc and file caching. by inheriting Cache_Abstract, a third-party caching tool can be called.

Refer to the cache class and apc of shindig.


 IsLocked ($ key) {return $ this;} $ tries = 10; $ count = 0; do {usleep (200); $ count ++ ;} while ($ count <= $ tries & $ this-> isLocked ($ key); // you can perform a maximum of ten sleep times and wait for unlocking, when timeout occurs, the system skips and unlocks $ this-> isLocked ($ key) & $ this-> unlock ($ key); return $ this ;}} /*** APC extension Cache implementation *** @ category Mjie * @ package Cache * @ author streamline Meng Chun * @ copyright Copyright (c) 2008-
 
  
* @ License New BSD License * @ version $ Id: Cache/Apc. php version 2010-04-18 cmpan $ */class Cache_Apc extends Cache_Abstract {protected $ _ prefix = 'cache .mjie.net '; public function _ construct () {if (! Function_exists ('apc _ cache_info ') {throw new CacheException ('apc extension didn' t installed ');}} /*** save the cache variable ** @ param string $ key * @ param mixed $ value * @ return bool */public function store ($ key, $ value) {return apc_store ($ this-> _ storageKey ($ key), $ value );} /*** read cache ** @ param string $ key * @ return mixed */public function fetch ($ key) {return apc_fetch ($ this-> _ storageKey ($ key);}/*** clear cache ** @ return Cache_Apc */public function clear () {apc_clear_cache (); return $ this;}/*** delete cache unit ** @ return Cache_Apc */public function delete ($ key) {apc_delete ($ this-> _ storageKey ($ key); return $ this ;} /*** whether the cache unit is locked ** @ param string $ key * @ return bool */public function isLocked ($ key) {if (apc_fetch ($ this-> _ storageKey ($ key ). '. lock') = false) {return false;} return true ;} /*** lock the cache unit ** @ param string $ key * @ return Cache_Apc */public function lock ($ key) {apc_store ($ this-> _ storageKey ($ key ). '. lock', '', 5); return $ this ;} /*** unlock the cache unit ** @ param string $ key * @ return Cache_Apc */public function unlock ($ key) {apc_delete ($ this-> _ storageKey ($ key ). '. lock'); return $ this;}/*** complete cache name ** @ param string $ key * @ return string */private function _ storageKey ($ key) {return $ this-> _ prefix. '_'. $ key ;}/ *** file Cache implementation *** @ category Mjie * @ package Cache * @ author streamline Mengchun * @ copyright Copyright (c) 2008-
  
   
* @ License New BSD License * @ version $ Id: Cache/File. php version cmpan $ */class Cache_File extends Cache_Abstract {public $ useSubdir = false; protected $ _ cachesDir = 'cache'; public function _ construct () {if (defined ('data _ dir') {$ this-> _ setCacheDir (DATA_DIR. '/cache') ;}}/*** get cache file ** @ param string $ key * @ return string */protected function _ getCacheFile ($ key) {$ subdir = $ This-> useSubdir? Substr ($ key, 0, 2 ). '/': ''; return $ this-> _ cachesDir. '/'. $ subdir. $ key. '. php ';}/*** read cache variable * to prevent information leakage, the cache file format is php file, and"
   "Start with ** @ param string $ key cache subscript * @ return mixed */public function fetch ($ key) {$ cacheFile = self: _ getCacheFile ($ key ); if (file_exists ($ cacheFile) & is_readable ($ cacheFile) {// include method // return include $ cacheFile; // serialization method return unserialize (@ file_get_contents ($ cacheFile, false, NULL, 13);} return false;}/*** cache variable * to prevent information leakage, the cached file is in the PHP file format and"
   "Start with ** @ param string $ key: cache variable subscript * @ param string $ value: cache variable value * @ return bool */public function store ($ key, $ value) {$ cacheFile = self: _ getCacheFile ($ key); $ cacheDir = dirname ($ cacheFile); if (! Is_dir ($ cacheDir) {if (! @ Mkdir ($ cacheDir, 0755, true) {throw new CacheException ("cocould not make cache directory ");}} // use the include method // return @ file_put_contents ($ cacheFile ,'
   '. Serialize ($ value);}/*** delete cache variable ** @ param string $ key cache subscript * @ return Cache_File */public function delete ($ key) {if (emptyempty ($ key) {throw new CacheException ("Missing argument 1 for Cache_File: delete ()");} $ cacheFile = self :: _ getCacheFile ($ key); if (! @ Unlink ($ cacheFile) {throw new CacheException ("Cache file cocould not be deleted") ;}return $ this ;} /*** whether the cache unit has been locked ** @ param string $ key * @ return bool */public function isLocked ($ key) {$ cacheFile = self :: _ getCacheFile ($ key); clearstatcache (); return file_exists ($ cacheFile. '. lock');}/*** lock ** @ param string $ key * @ return Cache_File */public function lock ($ key) {$ cacheFile = self: _ g EtCacheFile ($ key); $ cacheDir = dirname ($ cacheFile); if (! Is_dir ($ cacheDir) {if (! @ Mkdir ($ cacheDir, 0755, true) {if (! Is_dir ($ cacheDir) {throw new CacheException ("cocould not make cache directory") ;}}// sets the access and modification time of the cache lock file @ touch ($ cacheFile. '. lock'); return $ this;}/*** unlock ** @ param string $ key * @ return Cache_File */public function unlock ($ key) {$ cacheFile = self:: _ getCacheFile ($ key); @ unlink ($ cacheFile. '. lock'); return
  
 

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.