PHP Cache File Cache
1, PHP file cache content Save format
PHP file cache Content storage format is mainly three kinds:
(1) Variable var_export formatted into PHP normal assignment writing format;
(2) The variable serialize is stored after serialization and is deserialized when used;
(3) The variable json_encode is saved after formatting, when used Json_decode
The test results on the Internet are as follows: The parsing efficiency of serialize format is greater than that of Json,json, which is higher than php normal assignment.
So if we cache the data, we suggest that parsing the data in serialized form is faster.
2. Simple case of PHP file cache
_cache_path = $config [' Cache_path '];} else{$this->_cache_path = Realpath (dirname (__file__). " /")." /cache/";}} Determines whether the file corresponding to the key value exists, if present, reads the value, value to serialize the 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 F Alse;} return $data [' data ']; Set the cache information, generate the corresponding cache file according to the key value the public function set ($id, $data, $ttl =) {$contents = array (' time ' = "time"), ' ttl ' = = $ttl , ' data ' $data), if (@file_put_contents ($this->_cache_path. $id, serialize ($contents))) {@chmod ($this->_ Cache_path. $id, 0777); return TRUE;} return FALSE;} Delete the cache file Public function Delete ($id) {return @unlink ($this->_cache_path. $id) According to the key value;} 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;}}
http://www.bkjia.com/PHPjc/871195.html www.bkjia.com true http://www.bkjia.com/PHPjc/871195.html techarticle PHP Cache file cache 1, PHP file cache content save php file cache content of the main three kinds: (1) variable var_export into PHP normal writing style; (2) Variable ...