Php simple data cache class, php Data cache. Php simple data cache class, php Data cache company mobile phone touch screen station, because there are too many page images, so you need to do data cache, just write a data cache class. Directly paste code? P php simple data cache class, php Data cache
The company's mobile phone touch screen station, because there are too many page images, so you need to do data cache, just write a data cache class.
Directly paste code
/**
*
* Fianl_m@foxmail.com
* Cache class
* Query the data and serialize the data into a file.
**/
Class Cache {
Function _ construct ($ config ){
// Define whether cache is enabled
$ 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 the cached file
Public function open ($ name ){
$ Arr = array ();
$ Filename = $ this-> cache_file. $ name;
$ Status = filemtime ($ filename) + $ this-> cache_time> time (); // defines the cache time
If (file_exists ($ filename) & $ status & $ this-> is_cache ){
$ Content = file_get_contents ($ filename); // read the cached file
$ Arr = unserialize ($ content );
Return $ arr;
} Else {
Return false;
}
}
// Write the cached file
Public function write ($ name, $ data = array ()){
$ Filename = $ this-> cache_file. $ name;
$ Content = serialize ($ data );
File_put_contents ($ filename, $ content); // write the cached file
}
}
?>
In fact, it is nothing more than to serialize the select array into the text and then read it out.
Usage
// Define whether the cache is enabled
Require ('cache. class. php ');
$ Config = array (
'Is _ cache' => 1, // whether to enable the cache
'Cache _ file' => './cache/', // cache folder
'Cache _ time' => '60', // cache time
);
$ Cache = new Cache ($ config );
// Open the cache and input the cache file name
$ Row = $ cache-> open ($ filename );
// Write the file name and data (array) into the cache)
$ Cache-> write ($ filename, $ data );
Ps: If you don't understand it, you can leave a message for me. do not spray it. you can bypass it, and Cainiao will learn it!
Vivo mobile phone touch-screen station, because there are too many page images, so you need to do data cache, just write a data cache class. Directly paste code? P...