PHP file cache (improved)-PHP source code

Source: Internet
Author: User
Tags glob
Php file cache (improved:
During the development of MooPHP, in order to find a more efficient cache method, the two most common cache methods were tested.

Common PHP Cache methods:
First, process the data to be cached to form a file that can be directly executed by PHP. When data needs to be cached, it is introduced and used in the include mode.
Second, serialize the required data through the serialize function and save it directly to the file. When you need to use cached data, read the file content through deserialization, copy the content to the required variable, and then use it.

Test results:
Through tests, we found that the second method, namely serialize, caches data more efficiently. (The data is omitted, and finally the article address is provided for download. you can test it on your own)

Click to view

We accept others' suggestions to improve the memcache operation method for this class.
Support for serialize storage
Supports two storage methods: executable files

 ". /", 'File _ name_prefix' => 'cache', 'mode' => '1', // mode 1 is serialize model 2 and saved as an executable file ); /*** get this class instance ** @ return Ambiguous */public static function getInstance () {if (self ::$ _ instance === null) {self :: $ _ instance = new self ();} return self: $ _ instance ;} /*** get cache information ** @ param string $ id * @ return boolean | array */public static function get ($ id) {$ instance = self :: getInstance (); // The cache file does not exist. F (! $ Instance-> has ($ id) {return false;} $ file = $ instance-> _ file ($ id ); $ data = $ instance-> _ fileGetContents ($ file); if ($ data ['expire '] = 0 | time () <$ data ['expire']) {return $ data ['tents'];} return false ;} /*** set a cache ** @ param string $ id cache id * @ param array $ data cache content * @ param int $ cacheLife cache life is 0 infinite by default */public static function set ($ id, $ data, $ cacheLife = 0) {$ instance = self: getIns Tance (); $ time = time (); $ cache = array (); $ cache ['tents'] = $ data; $ cache ['expire '] = $ cacheLife = 0? 0: $ time + $ cacheLife; $ cache ['mtime'] = $ time; $ file = $ instance-> _ file ($ id ); return $ instance-> _ filePutContents ($ file, $ cache );} /*** clear a cache ** @ param string cache id * @ return void */public static function delete ($ id) {$ instance = self: getInstance (); if (! $ Instance-> has ($ id) {return false;} $ file = $ instance-> _ file ($ id ); // delete the cache return unlink ($ file );} /*** determine whether the cache exists ** @ param string $ id cache_id * @ return boolean true the cache exists false the cache does not exist */public static function has ($ id) {$ instance = self: getInstance (); $ file = $ instance-> _ file ($ id); if (! Is_file ($ file) {return false;} return true ;} /*** obtain the cache information path through the cache id * @ param string $ id * @ return string cache file path */protected function _ file ($ id) {$ instance = self:: getInstance (); $ fileNmae = $ instance-> _ idToFileName ($ id); return $ instance-> _ options ['cache _ dir']. $ fileNmae;}/*** obtain the cache information storage file name through id ** @ param $ id * @ return string cache file name */protected function _ idToFileName ($ id) {$ instance = self: getInstance (); $ prefix = $ instance-> _ options ['File _ name_prefix']; return $ prefix. '---'. $ id;}/*** get the cache id through filename ** @ param $ id * @ return string cache id */protected function _ fileNameToId ($ fileName) {$ instance = self: getInstance (); $ prefix = $ instance-> _ options ['File _ name_prefix']; return preg_replace ('/^ '. $ prefix. '---(. *) $/',' $ 1', $ fileName );} /*** write data to the file ** @ param string $ file name * @ param array $ contents data content * @ return bool */protected function _ filePutContents ($ file, $ contents) {if ($ this-> _ options ['mode'] = 1) {$ contents = serialize ($ contents );} else {$ time = time (); $ contents ="
 ";}$ Result = false; $ f =@ fopen ($ file, 'w'); if ($ f) {@ flock ($ f, LOCK_EX ); fseek ($ f, 0); ftruncate ($ f, 0); $ tmp = @ fwrite ($ f, $ contents); if (! ($ Tmp = false) {$ result = true ;}@ fclose ($ f) ;}@ chmod ($ file, 0777); return $ result ;} /*** get data from a file ** @ param sring $ file * @ return boolean | array */protected function _ fileGetContents ($ file) {if (! Is_file ($ file) {return false;} if ($ this-> _ options ['mode'] = 1) {$ f = @ fopen ($ file, 'R'); @ $ data = fread ($ f, filesize ($ file); @ fclose ($ f); return unserialize ($ data );} else {return include $ file; }}/ *** constructor */protected function _ construct () {}/*** set the cache path ** @ param string $ path * @ return self */public static function setCacheDir ($ path) {$ instance = self :: getInstance (); if (! Is_dir ($ path) {exit ('File _ cache: '. $ path.' is not a valid path ');} if (! Is_writable ($ path) {exit ('File _ cache: path "'. $ path. '"not writable');} $ path = rtrim ($ path ,'/'). '/'; $ instance-> _ options ['cache _ dir'] = $ path; return $ instance ;} /*** set the cache file prefix ** @ param srting $ prefix * @ return self */public static function setCachePrefix ($ prefix) {$ instance = self: getInstance (); $ instance-> _ options ['File _ name_prefix'] = $ prefix; return $ instance ;} /*** set the cache storage type ** @ param int $ mode * @ return self */public static function setCacheMode ($ mode = 1) {$ instance = self :: getInstance (); if ($ mode = 1) {$ instance-> _ options ['mode'] = 1 ;} else {$ instance-> _ options ['mode'] = 2;} return $ instance ;} /*** delete all caches * @ return boolean */public static function flush () {$ instance = self: getInstance (); $ glob = @ glob ($ instance-> _ options ['cache _ dir']. $ instance-> _ options ['File _ name_prefix']. '-- *'); if (empty ($ glob) {return false;} foreach ($ glob as $ v) {$ fileName = basename ($ v ); $ id = $ instance-> _ fileNameToId ($ fileName); $ instance-> delete ($ id) ;}return true ;}} /* initialize and set the cache configuration information or something */cache: setCachePrefix ('core'); // Set the cache file prefix cache: setCacheDir ('. /cache '); // set the path for storing the cache folder // mode 1 cache storage method // a: 3: {s: 8: "contents"; a: 7: {I: 0; I: 1; I: 1; I: 2; I: 2; I: 3; I: 3; I: 34; I: 4; I: 5; I: 5; I: 6; I: 6; I: 6;} s: 6: "expire"; I: 0; s: 5: "mtime "; i: 1318218422;} // mode 2 cache storage mode /*
 Array (0 => 1, 1 => 2, 2 => 3, 3 => 34, 4 => 5, 5 => 6, 6 => 6 ,), 'expire '=> 0, 'mtime' => 1318224645,)?> * **/Cache: setCacheMode ('2'); if (! $ Row = cache: get ('zj1') {$ array = array (, 6); $ row = cache: set ('zj1 ', $ array);} // cache: flush (); clear all cache print_r ($ row );

The above is the PHP file cache (improved) content. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.