This article mainly introduces the PHP file caching class Usage, the example form more detailed analysis of PHP file cache class definition, function and specific use skills, very practical value, the need for friends can refer to the
This example describes the usage of PHP file caching classes. Share to everyone for your reference. Specifically as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
<?php/** * Simple File Cache class * * */class xzcache{//default cache time one hour var $cache _time = 3600;//Default Cache dir var $cache _dir = './cache '; Public function __construct ($cache _dir=null, $cache _time=null) {$this->cache_dir = isset ($cache _dir)? $cache _dir: $ this->cache_dir; $this->cache_time = isset ($cache _time)? $cache _time: $this->cache_time; The Public Function Savecache ($key, $value) {if (Is_dir ($this->cache_dir)) {$cache _file = $this->cache_dir. '/xzcache_ '. MD5 ($key); $timedif = @ (Time ()-Filemtime ($cache _file)); if ($timedif >= $this->cache_time) {//cached file is too old, create new $serialized = Serialize ($value); if ($f = @fopen ($cache _file, ' W ')) {fwrite ($f, $serialized, strlen ($serialized)); Fclose ($f); }} $result = 1; }else{echo "Error:dir is isn't exist." $result = 0;} return $result; /** * @return Array * 0 No Cache * 1 Cached * 2 overdue/Public function GetCache ($key) {$cache _file = $this->CAC He_dir. '/xzcache_ '. MD5 ($key); if (Is_dir ($this->cache_dir) && is_file ($cache _file)) {$timedif = @ (Time ()-Filemtime ($cache _file)); if ($ti Medif >= $this->cache_time) {$result [' cached '] = 2;} else{//Cached file is fresh enough, return cached array $result [' value '] = Unserialize (file_get_contents ($cache _file)); $result [' cached '] = 1; }}else {echo "Error:no cache"; $result [' cached '] = 0;} return $result; }//end of Class |
Examples of usage are as follows:
?
1 2 3 4 5 6 |
$cache = new Xzcache (); $key = ' global '; $value = $GLOBALS; $cache->savecache ($key, $value); $result = $cache->getcache ($key); Var_dump ($result); |
I hope this article will help you with your PHP program design.