初识PHP文件缓存

  在ftruncate()函数的示例代码中看到一个清除文件缓存的函数clearstatcache(),不解,之前在c语言的一个小程序中也看到了清空缓冲区,想借此机会整理学习一下php文件缓存的知识,百度一下,找到了一篇比较浅显的文章,整理如下:  原文链接:这里  为什么要使用文件缓存?  假设我们程序的配置信息是放在数据库中的,每次程序执行都要到数据库去取配置信息,但配置信息又是不经常修改的,每次重数据库读记录是不是很慢呢.下面就让我来看看文件缓存的威力吧!$link =mysql_connect('l
摘要由CSDN通过智能技术生成

 

  在ftruncate()函数的示例代码中看到一个清除文件缓存的函数clearstatcache(),不解,之前在c语言的一个小程序中也看到了清空缓冲区,想借此机会整理学习一下php文件缓存的知识,百度一下,找到了一篇比较浅显的文章,整理如下:

  原文链接:这里

  为什么要使用文件缓存?

  假设我们程序的配置信息是放在数据库中的,每次程序执行都要到数据库去取配置信息,但配置信息又是不经常修改的,每次重数据库读记录是不是很慢呢.
下面就让我来看看文件缓存的威力吧!
<?php
$link =mysql_connect('localhost', 'root', '');                 //链接数据库
mysql_select_db('morrow');                                         //设置活动的 MySQL 数据库
mysql_query("SET character_set_connection='utf8', character_set_results='utf8', character_set_client=binary") or exit(mysql_error());                                                                                        //执行查询语句
$sql  = 'SELECT * FROM config';                               //定义sql语句
$res  = mysql_query($sql);                                        //查询
while($row = mysql_fetch_array($res, MYSQL_ASSOC))                    //循环执行
{
    $rows[] = $row;
}
//以上代码就讲了, 相信大家都明白
//var_export 函数是干什么的, 来看看官方的解释
//mixed var_export ( mixed expression [, bool return])
//此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码。
//您可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示。
$cache = "<?php /r/n /$CACHE = " . var_export($rows ,TRUE) . " ; /r/n ?>";
$file  = './cache.php';
file_put_contents($file, $cache);   //写入缓存
//除了var_export方法我们还可以使用另外一个方法就是使用 serialize 序列化函数
//使用的使用只要unserialize 反序列化 就行了
?>
这样我们以后要使用到配置信息直接include cache.php 文件就行了, 在修改配置文件时重新修改缓存文件就行了
这样就减少了对数据库访问的次数, 也就达到了目的.  修改配置文件时修改缓存文件, 这个同学们自己想下怎么做吧!

  找到一些与缓存相关的函数

 glob ( 匹配模式, 系统常量可为空)    寻找与模式匹配的文件路径 我觉得这个函数很有用很有用

  print_r(glob('*.*'));  这样将打印出你当前目录下的所有文件

  print_r(glob('*'));  所有文件及文件夹名称

  print_r(glob('./include/*.php')); include文件夹下的所有php文件

 

  is_numeric( 需要判断的变量 )   判断是不是数字或数字字符串
  $str = 1;
  echo is_numeric($str);  返回true
  $str = '4332829348';
  echo is_numeric($str); 返回true
  $str = '4332829348';
  echo is_numeric($str); 返回true
  $str = '123a';

  echo is_numeric($str); 返回false

又找到一个php缓存类,如下:

 

功能很简单,就是缓存整个页面,可以设定缓存时间,可以缓存特定的URL,例如:test.php?id=12,当目标文件更新时,如test.php,缓存文件也会更新,即使仍处于缓存期内。

 

 

 

class cache

  {

    var $cache_dir = './cache/';//This is the directory where the cache files will be stored;

    var $cache_time = 120;//How much time will keep the cache files in seconds.

 

    var $caching = false;

    var $file = '';

 

    function cache()

      {

      //Constructor of the class

      $this->file = $this->cache_dir . urlencode( $_SERVER['REQUEST_URI'] );

      if(file_exists($this->file)) $expired = $this->check_expire();

        else $expired = false;

      if ( file_exists ( $this->file ) && ( filemtime ( $this->file ) + $this->cache_time ) > time() && !$expired )

        {

          //Grab the cache:

          $handle = fopen( $this->file , "r");

          do {

            $data = fread($handle, 8192);

            if (strlen($data) == 0) {

            break;

          }

echo $data;

} while (true);

fclose($handle);

exit();

}

else

{

//create cache :

$this->caching = true;

ob_start();

$now = time();

echo "<!--last modified:".$now."-->/n";

}

}

 

function close()

{

//You should have this at the end of each page

if ( $this->caching )

{

//You were caching the contents so display them, and write the cache file

$data = ob_get_clean();

echo $data;

$fp = fopen( $this->file , 'w' );

fwrite ( $fp , $data );

fclose ( $fp );

}

}

function check_expire(){

$fp = fopen($this->file,"r");

preg_match("//:([/d]+)/-/",fread($fp,200),$time);

$modify_time = $time[1];

if($modify_time<filemtime($_SERVER['SCRIPT_FILENAME'])){

return true;

}

else{

return false;

}

 

}

}

 

 

 

用法:

//Example :
$ch = new cache();
echo date("D M j G:i:s T Y");
$ch->close();

额,看到一篇文章,好像文件缓存还有三种格式,整理如下:

 

  PHP文件缓存内容保存格式主要有三种:

  1.变量 var_export 格式化成PHP正常的赋值书写格式,用的时候直接include文件

  2.变量 serialize 序列化之后保存,用的时候反序列化

  3,变量 json_encode格式化之后保存,用的时候json_decode

  一直以来,我都以为第一种效率最高,因为那是PHP脚本解释器解析PHP脚本的格式,原生的,应该最快,至少读取缓存的效率应该是最高的,可是今天做了个测试,令我大跌眼镜!原来 serialize序列化效率才是最高的,不论是读还是写!

  下面是用来测试的PHP文件缓存的代码:

  1. $st = microtime(1);  
  2. for ($i=0;$i<1000;$i++){  
  3.     /* 
  4.     $file = var_export($_SERVER,1); 
  5.     $file = ""; 
  6.     file_put_contents("data/in.php",$file); 
  7.     */  
  8.     include("data/in.php");  
  9. }  
  10. echo "include读:".(microtime(1)-$st)." ";  
  11.   
  12. $st = microtime(1);  
  13. for ($i=0;$i<1000;$i++){  
  14.     $file = file_put_contents("data/se.php",serialize($_SERVER));  
  15.     //$file = file_get_contents("data/se.php");  
  16.     //$file = unserialize($file);  
  17. }  
  18. echo "serialize写:".(microtime(1)-$st)." ";  
  19. $st = microtime(1);  
  20. for ($i=0;$i<1000;$i++){  
  21.     //$file = file_put_contents("data/se.php",serialize($_SERVER));  
  22.     $file = file_get_contents("data/se.php");  
  23.     $file = unserialize($file);  
  24. }  
  25. echo "serialize读:".(microtime(1)-$st)." ";  
  26.   
  27. $st = microtime(1);  
  28. for ($i=0;$i<1000;$i++){  
  29.     $file = file_put_contents("data/js.php",json_encode($_SERVER));  
  30.     //$file = file_get_contents("data/js.php");  
  31.     //$file = json_decode($file);  
  32. }  
  33. echo "json写:".(microtime(1)-$st)." ";  
  34. $st = microtime(1);  
  35. for ($i=0;$i<1000;$i++){  
  36.     //$file = file_put_contents("data/js.php",json_encode($_SERVER));  
  37.     $file = file_get_contents("data/js.php");  
  38.     $file = json_decode($file);  
  39. }  
  40. echo "json读:".(microtime(1)-$st)." ";  
结果太神奇了!include写:0.559882879257
include读:0.185745000839
serialize写:0.255033969879
serialize读:0.0853068828583
json写:0.284864902496

json读:0.145938873291

序列化是最快,无论读或写,都是第一种的效率的两倍,json比序列化的PHP文件缓存效率稍低,表现还可以!

如果撇开文件读写所耗费的时间,他们的效率差别可能会更大!

include那个,虽然是PHP脚本赋值的格式,但是也是要分析解析文本,PHP脚本解释器需要动用整个解释器分析PHP脚本,而序列化不需要,只用启用序列化文本分析就行了,所以效率更高。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值