Php implements file data caching and code caching. Every time you access a page, it first checks whether the corresponding cache exists. If it does not exist, it connects to the database to obtain data, complete the template variable assignment, display the page, and generate cache files, so that the cached files will play a role in the next access.
Php tutorial code for file data caching
The cache technology checks whether the corresponding cache exists every time you access the page. If it does not exist, connect to the database tutorial to obtain data, assign values to template variables, and display the page, the cache file is generated at the same time, so that the cache file will play a role the next time you access it, instead of executing the if block data query statement. Of course, there are many things to consider in actual use, such as setting the validity period and the cache group.
<? Php
Class cacheexception extends exception {}
/**
* Cache abstract class
*/
Abstract class cache_abstract {
/**
* Read cache Variables
*
* @ Param string $ key cache subscript
* @ Return mixed
*/
Abstract public function fetch ($ key );
/**
* Cache Variables
*
* @ Param string $ key: cache variable subscript
* @ Param string $ value the cached variable value
* @ Return bool
*/
Abstract public function store ($ key, $ value );
/**
* Delete cache Variables
*
* @ Param string $ key cache subscript
* @ Return cache_abstract
*/
Abstract public function delete ($ key );
/**
* Clear (delete) All caches
*
* @ Return cache_abstract
*/
Abstract public function clear ();
/**
* Lock cache Variables
*
* @ Param string $ key cache subscript
* @ Return cache_abstract
*/
Abstract public function lock ($ key );
/**
* Unlock cache Variables
*
* @ Param string $ key cache subscript
* @ Return cache_abstract
*/
Abstract public function unlock ($ key );
/**
* Whether the cache variable is locked
*
* @ Param string $ key cache subscript
* @ Return bool
*/
Abstract public function islocked ($ key );
/**
* Make sure that the instance is not locked.
* You can perform a maximum of $ tries sleep waiting for unlocking. If the time-out period expires, the system skips and unlocks the tries.
*
* @ Param string $ key cache subscript
*/
Public function checklock ($ key ){
If (! $ This-> 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 tasks and wait for unlocking. If the time-out period is exceeded and unlocked
$ This-> islocked ($ key) & $ this-> unlock ($ key );
Return $ this;
}
}
/**
* Apc extension cache implementation
*
*
* @ Category mjie
* @ Package cache
* @ Author streamline Meng Chun
* @ Copyright (c) 2008-<cmpan (at) qq.com>
* @ 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 cache Variables
*
* @ 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 A 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;
}
/**
* Cache unit unlocking
*
* @ 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 Meng Chun
* @ Copyright (c) 2008-<cmpan (at) qq.com>
* @ 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 ');
}
}
/**
* Getting cached files
*
* @ Param string $ key
* @ Return string
*/
Protected function _ getcachefile ($ key ){
$ Subdir = $ this-> usesubdir? Substr ($ key, 0, 2 ).'/':'';
Return $ this-> _ cachesdir. '/'. $ subdir. $ key. '. php ';
}
/**
* Read cache Variables
* To prevent information leakage, the cached file is in the PHP file format and uses "<? Php exit;?> Start"
*
* @ 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 Variables
* To prevent information leakage, the cached file is in the PHP file format and uses "<? Php exit;?> Start"
*
* @ Param string $ key: cache variable subscript
* @ Param string $ value the cached 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, '<? Php return '. var_export ($ value, true ).';');
Return @ file_put_contents ($ cachefile, '<? Php exit;?> '. Serialize ($ value ));
}
/**
* Delete cache Variables
*
* @ 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 is 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: _ getcachefile ($ 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 ");
}
}
}
// Set 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
Next, let's take a look at the code of the smarty cache file instance.
Let's take a look at the page cache function provided by smarty:
1 <? Php
2 require ('smarty. class. php ');
3 $ smarty = new smarty;
4 $ smarty-> caching = true;
5if (! $ Smarty-> is_cached ('index. tpl ')){
6 // no cache available, do variable assignments here.
7 $ contents = get_database_contents ();
8 $ smarty-> assign ($ contents );
9}
10 $ smarty-> display ('index. tpl ');
11?>
When the php cache technology works, when the program queries data, it serializes the corresponding results and saves them to the file. In the future, the same query statement can be used without directly querying the database, instead, it is obtained from the cache file. This improvement improves the program running speed.