File cache refers to the cache to generate a file, this file can be php,txt and so on, when I download access to determine the last build time, if more than we specified time and then regenerate again, or directly call the cache file, so you can reduce the MySQL database query.
Principle of File Caching
1. The data that needs to be cached is serialized directly to the file via the Serialize function. When you need to use cached data, read the contents of the file by deserializing it and copy it to the required variables, and then use the. Infrequently changed data can be written to the cache file.
File Cache Instance
index.php
| The code is as follows |
Copy Code |
|
Define (' Cache_root ', './');
Include_once ('./cache.func.php ');
$file = ' Qzp ';
$cacheFile = Getcachefilebyid ($file, ' info/');
Print_r (Readcache ($cacheFile));
$info = Array (
' Username ' = ' qzp ',
' Area_name ' = ' admin ',
' Mp_name ' = ' 1234 ',
' Gh_name ' = ' 5678 ');
Writecache ($cacheFile, $info);
cache.func.php file
function Arrayeval ($array, $level = 0) {
$space = ";
for ($i = 0; $i <= $level; $i + +) {
$space. = "T";
}
$evaluate = "Arrayn$space (n";
$comma = $space;
foreach ($array as $key = = $val) {
$key = is_string ($key)? ". Addcslashes ($key,"). ": $key;
$val =!is_array ($val) && (!preg_match ("/^-?[ 1-9]d*$/", $val) | | strlen ($val) > 12)? ". Addcslashes ($val,"). ": $val;
if (Is_array ($val)) {
$evaluate. = "$comma $key =". Arrayeval ($val, $level + 1);
} else {
$evaluate. = "$comma $key = $val";
}
$comma = ", n$space";
}
$evaluate. = "N$space)";
return $evaluate;
}
function Getcachefilebyid ($id, $pre = ' info/', $md 5=true) {
if ($MD 5) {
$MD 5id = MD5 ($id);
return cache_root. ' /'. $pre. substr ($md 5id,0,2). ' /'. substr ($md 5id,2,2). ' /'. $id;
}else{
return cache_root. ' /'. $pre. $id;
}
}
function Readcache ($filename, $time =0) {
if ($datas = @file_get_contents ($filename)) {
$datas = Unserialize ($datas);
if ($time < 1 | | (Time ()-$datas [' Time '] < $time)) {
return $datas [' data '];
}
}
return false;
}
function Writecache ($filename, $data) {
MakeDir (DirName ($filename));
if ($handle = fopen ($filename, ' w+ ')) {
$datas = Array (' data ' = $data, ' time ' =>time ());
Flock ($handle, LOCK_EX);
$rs = fputs ($handle, serialize ($datas));
Flock ($handle, lock_un);
Fclose ($handle);
if ($rs!==false) {
return true;
}
}
return false;
}
function MakeDir ($path)
{
if (! file_exists ($path)) {
MakeDir (DirName ($path));
if (! mkdir ($path, 0777))
Die (' Cannot create folder '. $path);
}
} |
Serialize the files to be cached, and then deserialize them back to use when they are used.
Use the Smarty tool to cache PHP template data. Smarty the HTML file into a static HTML page, the template file that needs to be cached can use Smarty.
Cases:
| The code is as follows |
Copy Code |
|
Contains the Smarty class library
Require (' libs/smarty.class.php ');
To create an object of the Smarty class
$smarty = new Smarty;
Enable caching
$smarty->caching = true;
Specify the directory where the cache file is saved
$smarty->cache_dir = "./cache/";
will also save the output
$smarty->display (' Index.tpl ')
?> |
http://www.bkjia.com/PHPjc/444641.html www.bkjia.com true http://www.bkjia.com/PHPjc/444641.html techarticle file cache refers to the cache generated a file, this file can be php,txt and so on, when I download access to determine the last generation of access time, if more than we specified ...