PHP Simple Data Cache class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.