PHP simple data cache class, PHP data cache_PHP tutorial

WBOY
Release: 2016-07-13 09:52:18
Original
935 people have browsed it

php simple data caching class, php data caching

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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1009707.htmlTechArticlephp simple data caching class, php data caching company mobile touch screen station, because there are too many pictures on the page, so it needs to be done For data caching, just write a data caching class. Paste the code directly ?p...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!