Home > Backend Development > PHP Tutorial > PHP cache file entry program_PHP tutorial

PHP cache file entry program_PHP tutorial

WBOY
Release: 2016-07-20 11:06:42
Original
1274 people have browsed it

class PageCache {

 /**
* @var string $file cache file address
* @access public
*/
 public $file;
 
 /**
* @var int $cacheTime cache time
* @access public
*/
 public $cacheTime = 3600;
 
 /**
* Constructor
* @param string $file cache file address
* @param int $cacheTime cache time
*/
 function __construct($file, $cacheTime = 3600) {
  $this->file = $file;
  $this->cacheTime = $cacheTime;
 }
 
 /**
* Get the cached content
* @param bool Whether to output directly, true to go directly to the cache page, false to return the cached content
* @return mixed
*/
 public function get($output = true) {
  if (is_file($this->file) && (time()-filemtime($this->file))<=$this->cacheTime && !$_GET['nocache']) {
   if ($output) {
    header('location:' . $this->file);
    exit;
   } else {
    return file_get_contents($this->file);
   }
  } else {
   return false;
  }
 }
 
 /**
* Set cache content
* @param $content content html string
*/
 public function set($content) {
  $fp = fopen($this->file, 'w');
  fwrite($fp, $content);
  fclose($fp);
 }
}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445011.htmlTechArticleclass PageCache { /*** @var string $file cache file address * @access public*/ public $file; /*** @var int $cacheTime cache time * @access public*/ public $cacheTime = 3600;...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template