Home > Backend Development > PHP Tutorial > PHP simple data caching class

PHP simple data caching class

WBOY
Release: 2016-08-08 09:23:30
Original
1170 people have browsed it

The company’s mobile touch screen website has too many pictures on the page, so 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 the cache directory
$this->cache_file=$config['cache_file'];
//Define the 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 the cache time
if( file_exists($filename) && $status && $this->is_cache){
$c//Read the cache file
$arr=unserialize( $content);
return $arr;
}else{
return false;
}
}
//Write cache file
public function write($name,$data=array()){
$filename=$this ->cache_file.$name;
$c
file_put_contents($filename, $content);//Write the cache file
}

}


?>

In fact, it is nothing more than putting the select array and then sequence into the text and then read it out.

Usage

//Defining whether the cache is turned on
require('cache.class.php');
$c /> 'is_cache'=>1,//Whether the cache is turned on
'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, please leave me a message. If you don’t understand, don’t criticize. Masters will bypass it, and novices will learn!

The above introduces the PHP simple data caching class, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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