The company's mobile touch screen station, because there are too many pictures on the page, data caching is needed, so just write a data caching class.
Post the code directly
/**
*
* fianl_m@foxmail.com
* Cache class
* Query the data and serialize it into the file
**/
class Cache{
function __construct($config){
//Define whether to enable caching
$ this->is_cache=$config['is_cache'];
//Define cache directory
$this->cache_file=$config['cache_file'];
//Define cache time
$this->cache_time=$config['cache_time'];
}
//Read cache file
public function open($name){
$arr=array();
$filename=$this->cache_file.$name;
$status=filemtime($filename) $this->cache_time>time(); //Define cache time
if( file_exists($filename) && $status && $this->is_cache){
$content=file_get_contents($filename);//Read cache file
$arr =unserialize($content);
return $arr;
}else{
return false;
}
}
//Write cache file
public function write($name,$data=array()){
$filename=$this->cache_file.$name;
$content=serialize($data);
file_put_contents($filename, $content);//Write cache file
}
}
?>
In fact, it is nothing more than serializing the select array into text and then reading it out.
How to use
//Defining whether the cache is enabled
require('cache.class.php');
$config=array(
'is_cache'=>1,//Whether the cache is enabled
'cache_file'=>'./cache/',//Cache folder
'cache_time'=>'60',//Cache time
);
$cache=new Cache($ config);
//Open the cache and pass in the cache file name
$row=$cache->open($filename);
//Write cache incoming file name and data (array)
$cache->write($filename,$data);
ps: If you don’t understand, you can leave me a message. Don’t criticize if it’s not your fault. Masters will bypass it and novices will learn!