PHP file cache Content Save format instance analysis, _php tutorial

Source: Internet
Author: User
Tags delete cache

PHP file cache Content Save format instance analysis,


This article describes the PHP file cache content saving format, for PHP project development is very practical value. Share to everyone for reference. The specific analysis is as follows:

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

<?phpclass Cache_driver {//define the path of the cache protected $_cache_path; Gets the path information according to the Cache_path value in $config public function cache_driver ($config) {if (Is_array ($config) && isset ($config [    ' Cache_path ']) {$this->_cache_path = $config [' Cache_path ']; } else {$this->_cache_path = Realpath (dirname (__file__). "/") .    "/cache/"; }}//Determine if the file corresponding to the key value exists, if present, read value value, value to serialize 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 FALSE;  } return $data [' data ']; }//Set the cache information, generate the corresponding cache file according to the key value, 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 cache file according to key value public function Delete ($id) {return @unlink ($this->_cache_path. $id);    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; }}

I hope that the PHP cache instance described in this article will help you to develop PHP program.




http://www.bkjia.com/PHPjc/866663.html www.bkjia.com true http://www.bkjia.com/PHPjc/866663.html techarticle PHP File Cache Content Preservation Format Instance analysis, this article describes the PHP file cache content saving format, for PHP project development is very practical value. Share to everyone ...

  • 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.