PHP cache file cache_PHP tutorial

WBOY
Release: 2016-07-13 10:19:47
Original
960 people have browsed it

PHP cache file cache

1. PHP file cache content saving format
There are three main formats for saving PHP file cache content:
(1) The variable var_export is formatted into PHP’s normal assignment writing format;
(2) The variable serialize is saved after serialization and deserialized when used;
(3) The variable json_encode is formatted and saved. When used, json_decode
The test results on the Internet are: the file parsing efficiency in serialize format is greater than Json, and the parsing efficiency of Json is greater than PHP normal assignment.
Therefore, if we cache data, it is recommended to use serialization to parse the data faster.

2. Simple case of PHP file caching

<?php
class Cache_Driver{
	//定义缓存的路径
	protected $_cache_path;

//根据$config中的cache_path值获取路径信息
	public function Cache_Driver($config)
	{
		if(is_array($config) && isset($config[&#39;cache_path&#39;]))
		{
		   $this->_cache_path = $config[&#39;cache_path&#39;];
		}
		else
		{
		   $this->_cache_path = realpath(dirname(__FILE__)."/")."/cache/";
		}
	}
//判断key值对应的文件是否存在,如果存在,读取value值,value以序列化存储
	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[&#39;time&#39;]) || !isset($data[&#39;ttl&#39;]))
		{
			return FALSE;
		}
		
		if ($data[&#39;ttl&#39;] > 0 && time() >  $data[&#39;time&#39;] + $data[&#39;ttl&#39;])
		{
			@unlink($this->_cache_path.$id);
			return FALSE;
		}
		
		return $data[&#39;data&#39;];
	}
//设置缓存信息,根据key值,生成相应的缓存文件
	public function set($id, $data, $ttl = 60)
	{		
		$contents = array(
				&#39;time&#39;		=> time(),
				&#39;ttl&#39;		=> $ttl,			
				&#39;data&#39;		=> $data
			);
		
		if (@file_put_contents($this->_cache_path.$id, serialize($contents)))
		{
			@chmod($this->_cache_path.$id, 0777);
			return TRUE;			
		}

		return FALSE;
	}
//根据key值,删除缓存文件
	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;
	}
}
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871195.htmlTechArticlePHP cache file cache 1. PHP file cache content saving method There are three main ways to save PHP file cache content: ( 1) The variable var_export is formatted into the normal PHP assignment format; (2) Variable...
Related labels:
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