PHP File Cache method summary,

Source: Internet
Author: User
Tags flock glob php memcached

PHP File Cache method summary,

We will share with you the full PHP File Cache for your reference. The specific content is as follows:

<? Php class cache {private static $ _ instance = null; protected $ _ options = array ('cache _ dir' => ". /", '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 (); // if (! $ 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 Component 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 = "<? Php \ n "." // mktime: ". $ time." \ n "." return ". var_export ($ contents, true)." \ n?> ";}$ 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;}/*** sets 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_p Refix']. '-- *'); 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: "ex Pire "; I: 0; s: 5:" mtime "; I: 1318218422;} // Mode 2 cache storage mode/* <? Php // mktime: 1318224645 return array ('contents' => 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 );

File Cache class

<? Php/*** FILE cache class * @ author xiaojiong & 290747680@qq.com * @ date 2011-08-17 */class cache {const FILE_LIFE_KEY = 'file _ LIFE_KEY '; const CLEAR_ALL_KEY = 'clear _ all'; static $ _ instance = null; protected $ _ options = array ('cache _ dir' => '. /cache', 'file _ locking' => true, 'file _ name_prefix' => 'cache', 'cache _ file_umask '=> 0777, 'file _ life' => 100000); static function & getInstance ($ options = array ()){ If (self ::$ _ instance === null) {self: $ _ instance = new self ($ options);} return self ::$ _ instance ;} /*** set the parameter * @ param array $ options cache parameter * @ return void */static function & setOptions ($ options = array () {return self :: getInstance ($ options);}/*** constructor * @ param array $ options cache parameter * @ return void */private function _ construct ($ options = array ()) {if ($ this-> _ options ['cache _ dir']! = Null) {$ dir = rtrim ($ this-> _ options ['cache _ dir'], '/'). '/'; $ this-> _ options ['cache _ dir'] = $ dir; if (! Is_dir ($ this-> _ options ['cache _ dir']) {mkdir ($ this-> _ options ['cache _ dir'], 0777, TRUE );} if (! Is_writable ($ this-> _ options ['cache _ dir']) {exit ('file _ cache: path "'. $ this-> _ options ['cache _ dir']. '"not writable');} else {exit ('file _ cache:" options "cache_dir cannot be blank ');}} /*** set the cache path * @ param string $ value * @ return void */static function setCacheDir ($ value) {$ self = & self: getInstance (); if (! Is_dir ($ value) {exit ('file _ cache: '. $ value.' is not a valid path ');} if (! Is_writable ($ value) {exit ('file _ cache: path "'. $ value. '"not writable');} $ value = rtrim ($ this-> _ options ['cache _ dir'], '/'). '/'; $ self-> _ options ['cache _ dir'] = $ value ;} /*** save cache data ** @ param array $ data put into cache data * @ param string $ id cache id (also known as cache identifier) * @ param cache_life cache time * @ return boolean True if no problem */static function save ($ data, $ id = null, $ cache_life = null) {$ self = & self:: getInstance (); if (! $ Id) {if ($ self-> _ id) {$ id = $ self-> _ id;} else {exit ('file _ cache: save () id cannot be blank! ') ;}}$ Time = time (); if ($ cache_life) {$ data [self: FILE_LIFE_KEY] = $ time + $ cache_life;} elseif ($ cache_life! = 0) {$ data [self: FILE_LIFE_KEY] = $ time + $ self-> _ options ['file _ life'];} $ file = $ self-> _ file ($ id); $ data = "<? Php \ n "." // mktime: ". $ time." \ n "." return ". var_export ($ data, true)." \ n?> "; $ Res = $ self-> _ filePutContents ($ file, $ data); return $ res ;} /*** get Cache Information ** @ param string $ id cache id * @ return string | array cached data */static function load ($ id) {$ self = & self:: getInstance (); $ time = time (); // checks whether the cache exists if (! $ Self-> test ($ id) {// The cache is not hit! Return false;} // clear all identified files $ clearFile = $ self-> _ file (self: CLEAR_ALL_KEY); $ file = $ self-> _ file ($ id ); // determine whether the cache has been cleared. if (is_file ($ clearFile) & filemtime ($ clearFile)> filemtime ($ file) {return false ;} $ data = $ self-> _ fileGetContents ($ file); if (empty ($ data [self: FILE_LIFE_KEY]) | $ time <$ data [self :: FILE_LIFE_KEY]) {unset ($ data [self: FILE_LIFE_KEY]); return $ data;} return false;}/*** write to the cache file ** @ Param string $ file Cache path * @ param string $ string cache information * @ return boolean true success */protected function _ filePutContents ($ file, $ string) {$ self = & self: getInstance (); $ result = false; $ f = @ fopen ($ file, 'AB +'); if ($ f) {if ($ self-> _ options ['file _ locking ']) @ flock ($ f, LOCK_EX); fseek ($ f, 0); ftruncate ($ f, 0); $ tmp = @ fwrite ($ f, $ string); if (! ($ Tmp = false) {$ result = true ;}@ fclose ($ f) ;}@ chmod ($ file, $ self-> _ options ['cache _ file_umask ']); return $ result ;} /*** formatted cache file path ** @ param string $ id cache id * @ return string cache file name (including path) */protected function _ file ($ id) {$ self = & self: getInstance (); $ fileName = $ self-> _ idToFileName ($ id ); return $ self-> _ options ['cache _ dir']. $ fileName;}/*** formatted cache file name ** @ param string $ id cache id *@ Return string cache file name */protected function _ idToFileName ($ id) {$ self = & self: getInstance (); $ self-> _ id = $ id; $ prefix = $ self-> _ options ['file _ name_prefix']; $ result = $ prefix. '---'. $ id; return $ result ;} /*** determine whether the Cache exists ** @ param string $ id Cache id * @ return boolean True the Cache exists False the Cache does not exist */static function test ($ id) {$ self = & self: getInstance (); $ file = $ self-> _ file ($ id); if (! Is_file ($ file) {return false;} return true ;} /*** get Cache Information ** @ param string $ file Cache path * @ return string cached content */protected function _ fileGetContents ($ file) {if (! Is_file ($ file) {return false;} return include $ file;}/*** clear all caches ** @ return void */static function clear () {$ self = & self: getInstance (); $ self-> save ('clear _ all', self: CLEAR_ALL_KEY );} /*** clear a cache ** @ param string cache id * @ return void */static function del ($ id) {$ self = & self: getInstance (); if (! $ Self-> test ($ id) {// return false does not exist in the cache;} $ file = $ self-> _ file ($ id ); return unlink ($ file );}}

Store Data

<? Php $ config = array ('name' => 'xiaojiong ', 'qq' => '000000', 'age' => '20 ',); // The first parameter caches data // The second parameter cache id // The third parameter cache_life 0 never expires (cache: clear () clear all except) the default cache_life is option_cache_lifecache :: save ($ config, 'config', 0 );

Load data

<? Php // only one parameter cache_id $ config = cache: load ('config ');

Clear Cache

<? Php // clear the specified cache: del ('config'); // clear all cache: clear ();

Cache Information Configuration

// Call $ _ options = array ('cache _ dir' => 'before executing all cache_func statements '. /cache', // cache file directory 'file _ name_prefix' => 'cache', // cache file prefix 'file _ life' => 100000, // cache file life); cache: setOptions ($ options); // the new configuration information will be executed after the execution. Otherwise, the default information cache: save ($ arr, 'arr'); // This method seems unreasonable.

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • PHP Memcached + APC + File Cache encapsulation implementation code
  • PHP File Cache performance test
  • Php File Cache Function
  • PHP File Cache to memory cache
  • PHP File Cache content storage format instance analysis
  • PHP File Cache class Summary
  • Summary of common php Data File Cache classes
  • PHP File Cache example sharing
  • PHP File Cache Usage instance analysis
  • ThinkPHP File Cache code sharing

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.