PHP Cache file cache _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP Cache file cache. PHP Cache file cache 1. PHP file cache content storage type mainly has three types: (1) variable var_export is converted into PHP normal assignment writing type; (2) variable PHP Cache file cache
1. php file cache content storage format
There are three main formats for saving php file cache content:
(1) var_export is formatted as a normal value assignment format in PHP;
(2) the variable serialize is saved after serialization and deserialized when used;
(3) the json_encode variable is formatted and saved. when used, json_decode
On the Internet, the test result is: the file parsing efficiency in serialize format is greater than that in Json format, and the Json parsing efficiency is greater than that in PHP normal assignment.
Therefore, we recommend that you parse the data in serialized form faster if the cached data is used.

2. simple php file cache case

 _ Cache_path = $ config ['cache _ path'];} else {$ this-> _ cache_path = realpath (dirname (_ FILE __). "/"). "/cache/" ;}/// determine whether the file corresponding to the key value exists. if so, read the value and value to serialize and store public function get ($ id) {if (! File_exists ($ this-> _ cache_path. $ id) {return FALSE;} $ data = @ file_get_contents ($ this-> _ cache_path. $ id); $ data = unserialize ($ data); if (! Is_array ($ data) |! Isset ($ data ['Time']) |! Isset ($ data ['ttl ']) {return FALSE;} if ($ data ['ttl']> 0 & time ()> $ data ['Time'] + $ data ['ttl ']) {@ unlink ($ this-> _ cache_path. $ id); return FALSE;} return $ data ['data'];} // sets the cache information and generates the public function set ($ id, $ data, $ ttl = 60) {$ contents = array ('time' => time (), 'ttl '=> $ ttl, 'data' => $ data ); if (@ file_put_contents ($ this-> _ cache_path. $ id, serialize ($ contents) {@ chmod ($ this-> _ cache_path. $ id, 077 7); return TRUE;} return FALSE;} // based on the key value, delete the cache file public function delete ($ id) {return @ unlink ($ this-> _ cache_path. $ id);} public function clean () {$ dh = @ opendir ($ this-> _ cache_path); if (! $ Dh) return FALSE; while ($ file = @ readdir ($ dh) {if ($ file = ". "| $ file = ".. ") continue; $ path = $ this-> _ cache_path. "/". $ file; if (is_file ($ path) @ unlink ($ path) ;}@ closedir ($ dh); return TRUE ;}}


Example 1: PHP file cache content retention type mainly has three types: (1) variable var_export type into PHP normal assignment writing type; (2) variable...

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.