Php simple data cache class, php Data cache _ PHP Tutorial

Source: Internet
Author: User
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...

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.