Company mobile phone touch screen station, because the page picture too much, so need to do data cache, just write a data cache class.
Direct Sticker Code
<?php
/**
*
* [Email protected]
* Cache Class
* Query out data and serialize write file
**/
Class cache{
function __construct ($config) {
Define whether to turn on caching
$this->is_cache= $config [' Is_cache '];
Defining the Cache Directory
$this->cache_file= $config [' Cache_file '];
Define Cache Time
$this->cache_time= $config [' cache_time '];
}
Reading cache files
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, the array of select is then serialized into the text and then read out.
How to use
Defines whether the cache is turned on
Require (' cache.class.php ');
$config =array (
' Is_cache ' =>1,//whether to turn on caching
' Cache_file ' = './cache/',//cache folder
' Cache_time ' = ' 60 ',//cache time
);
$cache =new Cache ($config);
Open cache, incoming cache file name
$row = $cache->open ($filename);
Write cache incoming file name and data (array)
$cache->write ($filename, $data);
PHP Simple Data Cache class