How to cache php variables in the form of files. _ PHP Tutorial

Source: Internet
Author: User
Cache php variables as files ,. This document describes how to cache php variables in the form of files. Share it with you for your reference. The specific implementation method is as follows: php * $ cache_s caches php variables as files,

This example describes how to cache php variables as files. Share it with you for your reference. The specific implementation method is as follows:

<? Php/* $ cache_set = array (// cache path, add "/" 'cacheroot '=> '. /cache/', // cache time 'cachetime' => 20, // cache type 'cachetype' => 1, // extension 'cacheexe '=> '. php '); $ cache = new Cache ($ cache_set); $ a = array ('1', '2'); $ a = "aaa "; $ B = ''; if ($ cache-> cache_is (" d ") {$ c = $ cache-> cache_read (" d "); echo" c "; print_r ($ c);} else {$ B = $ cache-> cache_data ('D', $ a);} print_r ($ B ); // $ cache-> clear ("a"); // echo $ cache-> cache_read (". /cache/d. php "); // echo $ D; * // **** data cache class v1.0 * @ author Shoke * 16:02:26 * used to cache data, such as variables, but the page cannot be cached */class Cache {// Configure public $ config = array (// Cache path 'cacherot' => '. /cache/', // cache time 'cachetime' => 1, // cache type 1 stringized Data 2 variable 'cachetype' => 2, // The extension 'cacheexe '=> '. php '// Convert the intermediate variable); public $ return_name = array (); function _ construct ($ cache_set = array () {if (! Empty ($ cache_set) $ this-> config = array_merge ($ this-> config, $ cache_set ); $ this-> config ['classname'] = _ CLASS __;} public function clear ($ filename = '') {if (file_exists ($ this-> cache_file ($ filename) {@ unlink ($ this-> cache_file ($ filename);} elseif (empty ($ filename )) {$ this-> clear_dir ($ this-> config ['cacherot']);} else {$ this-> clear_dir ($ this-> config ['cacherot']. $ filename); echo $ this-> config ['cache Root ']. $ filename ;}// the path to the private function clear_dir ($ dir, $ to = false) {if ($ list = glob ($ dir. '/*') {foreach ($ list as $ file) {is_dir ($ file )? $ This-> clear_dir ($ file): unlink ($ file) ;}} if ($ to === false) rmdir ($ dir );} // write cache private function cache_write ($ filename, $ writetext, $ openmod = 'w') {if (! File_exists ($ filename) {@ $ this-> makeDir (dirname ($ filename);} if (@ $ fp = fopen ($ filename, $ openmod )) {flock ($ fp, 2); fwrite ($ fp, $ writetext); fclose ($ fp); return true;} else {echo "File: $ filename write error. "; return false ;}// return true public function cache_is ($ fileName) {$ fileName = $ this-> cache_file ($ fileName ); if (file_exists ($ fileName) {// if the cache time is negative, it never expires if ($ this-> config [' Cachetime'] <0) {return true;} // if the cache time is 0, it will always expire if ($ this-> config ['cachetime'] = 0) {return false;} // Obtain the cache file creation time $ ctime = intval (filemtime ($ fileName); // compare whether it is later than the cache time, if (time ()-$ ctime> $ this-> config ['cachetime']) {return false;} else {return true ;} // if the file does not exist, it is deemed that it has expired.} else {return false;} public function cache_data ($ name, $ data) {$ varname = $ name; $ name = $ this-> cache_file ($ nam E); // config ['cachetime'] = 0. if cache is not enabled, data is directly returned if ($ this-> config ['cachetime'] <> 0) {if ($ this-> config ['cachetype '] = 1) {$ write_data = "<? Php exit;?> ". Serialize ($ data); // return $ data;} else {$ write_data =" <? Php \ r \ n \ $ var = "; $ write_data. = var_export ($ data, true); $ write_data. =" ;\\ r \ n?> ";}$ This-> cache_write ($ name, $ write_data) ;}return $ data ;}// cache file name private function cache_file ($ filename) {return $ this-> config ['cacherot']. $ filename. $ this-> config ['cacheexe '];} // read the public function cache_read ($ file) {$ file = $ this-> cache_file ($ file); if (! File_exists ($ file) {return '';} if ($ this-> config ['cachetype '] = 1) {if (function_exists ('File _ get_contents ')) {$ cache_Content = file_get_contents ($ file);} else {$ fopen = fopen ($ file, 'r'); $ cache_Content = ''; do {$ data = fread ($ fopen, filesize ($ file); if (strlen ($ data) = 0) break; $ cache_Content. = $ data;} while (1); fclose ($ fopen);} $ cache_Content = substr ($ cache_Content, 13);/* Remove <? Php exit;?> */$ Cache_Content = unserialize ($ cache_Content); return $ cache_Content;} else {include_once ($ file); return $ var ;}} // cyclically create the directory private function makeDir ($ dir, $ mode = 0777) {if (! $ Dir) return 0; $ dir = str_replace ("\\\\", "/", $ dir); $ mdir = ""; foreach (explode ("/", $ dir) as $ val) {$ mdir. = $ val. "/"; if ($ val = ".. "| $ val = ". "| trim ($ val) =" ") continue; if (! File_exists ($ mdir) {if (! @ Mkdir ($ mdir, $ mode) {return false ;}} return true ;}}?>

I hope this article will help you with php programming.

Example: This article describes how to cache php variables in the form of files. Share it with you for your reference. The specific implementation method is as follows: php/* $ cache_s...

Related Article

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.