VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 几个简单的php数据文件缓存类

数据文件缓存的做法我们常用的有php文件缓存与利用memcache来缓存数据,下面面我分别总结了memcache缓存数据与数据文件缓存有需要的朋友可参考参考.

1.对于一般的变量,把该变量变成php语言的格式,写到文件中,用时只要include这个文件就相当于加载了cache了.

2.对于array型的变量,把array转化为php语言定义array的字符串,写到文件中,用时也只要include就相当于加载了cache了.

3.缓存cache时间上的控制,通过获取缓存文件的创建时间和现在的时间进行对比,如果没有到更新时间,直接读取缓存,如果到了更新时间,查询数据库.

文件缓存类,代码如下:

  1. <?php 
  2. class DataCache 
  3. { 
  4.  
  5.  /** 
  6.   * 数组转换 
  7.   * 
  8.   * @param array $array 
  9.   * @param string $arrayName 
  10.   * @param array $level 
  11.   * 
  12.   * @return string 
  13.   */ 
  14.  private function arrayEval($array, $arrayName = '', $level = 0) 
  15.  { 
  16.   $space = str_repeat("t", $level); 
  17.  
  18.   if (emptyempty($arrayName)) 
  19.   { 
  20.    $evaluate = "arrayn$space(n"; 
  21.   } 
  22.   else 
  23.   { 
  24.    $evaluate = "${$arrayName} = arrayn$space(n"; 
  25.   } 
  26.  
  27.   $space2 = str_repeat("t", $level + 1); 
  28.   $comma = $space2; 
  29.   if (!emptyempty($array)) 
  30.   { 
  31.    foreach ($array as $key => $val) 
  32.    { 
  33.     $key = is_string($key) ? ''' . addcslashes($key, ''\') . ''' : $key; 
  34.     $val = !is_array($val) && (!preg_match('/^-?[1-9]d*$/', $val) || strlen($val) > 12) ? ''' . addcslashes($val, ''\') . ''' : $val; 
  35.     if (is_array($val)) 
  36.     { 
  37.      $evaluate .= "$comma$key => " . arrayEval($val, '', $level + 1); 
  38.     } 
  39.     else 
  40.     { 
  41.      $evaluate .= "$comma$key => $val"; 
  42.     } 
  43.     $comma = ",n$space2"; 
  44.    } 
  45.   } 
  46.   $evaluate .= "n$space)"; 
  47.  
  48.   // 最后才需要一个“;” 
  49.   if ($level == 0) 
  50.   { 
  51.    $evaluate .= ";"; 
  52.   } 
  53.   return $evaluate; 
  54.  } 
  55.  
  56.  /** 
  57.   * 写入缓存 
  58.   * 
  59.   * @param string $path 
  60.   * @param string $arrayName 
  61.   * @param array  $data 
  62.   * 
  63.   * @return boolean 
  64.   */ 
  65.  public static function writeCache($path, $arrayName, $data) 
  66.  { 
  67.   if ($handle = fopen($path, 'w+')) 
  68.   { 
  69.    $data = self::arrayEval($data, $arrayName); 
  70.  
  71.    $dataConvert = "<?phpn" . $data; 
  72.  
  73.    flock($handle, LOCK_EX); 
  74.    $rs = fputs($handle, $dataConvert); 
  75.    flock($handle, LOCK_UN); 
  76.    fclose($handle); 
  77.    if ($rs !== false) 
  78.    { 
  79.     return true; 
  80.    } 
  81.   } 
  82.   return false; 
  83.  } 
  84.  
  85. } 
  86. ?> 

调用方法,代码如下:

  1. /** 
  2. * 生成文件缓存 
  3. * 
  4. * @param string $filePath 缓存文件的保存路径 
  5. * @param string $arrayName 存放在缓存文件中的数组名称 
  6. * @param array $data 数据 
  7. * 
  8. * @return boolean 
  9. */ 
  10. DataCache::writeCache($filePath, $arrayName, $data); 

memcache来缓存数据,提供这个文件缓存的类m希望大家可以看看,代码如下:

  1. <?php 
  2. /** 
  3.  * 文件缓存类 
  4.  * 提供文件缓存 
  5.  * @author guoyubin(263421949@qq.com) 
  6.  */ 
  7. class Cache_FileCache{ 
  8.      
  9.     /** 
  10.      * 设置缓存 
  11.      * @param $key 缓存的关键字key 
  12.      * @param $data 缓存的内容 
  13.      * @param $cacheLife 缓存时间(单位为秒)如果为0 则表示无限时间 
  14.      * @return Bool 
  15.      */ 
  16.     public static function setCache($key,$data,$cacheLife) 
  17.     { 
  18.             if(file_exists(__SITE_FILE_CACHE)) 
  19.             { 
  20.                 @$file                =  __SITE_FILE_CACHE . "/" . $key .".php"; 
  21.                 $cache                  =  array(); 
  22.                 $time                =  __SYS_TIME; 
  23.                 $cache['content']    =  $data; 
  24.                 $cache['expire']    =  $cacheLife === 0 ? 0 : $time+$cacheLife; 
  25.                 $cache['mtime']        =  $time; 
  26.                 $cache                =  serialize($cache); 
  27.                 $setReslut            =  @file_put_contents($file,$cache) or self::error(__line__,"文件写入出错"); 
  28.                 $chmodReslut        =  @chmod($file,0777) or self::error(__line__,"设定文件权限失败"); 
  29.                 if($setReslut && $chmodReslut) 
  30.                 { 
  31.                     return true; 
  32.                 } 
  33.                 else 
  34.                 { 
  35.                     return false; 
  36.                 } 
  37.                  
  38.             } 
  39.          
  40.     } 
  41.  
  42.     /** 
  43.      * 得到缓存数据 
  44.      * @param $key 缓存的关键字key 
  45.      * @return array 
  46.      */ 
  47.     public static function getCache($key) 
  48.     { 
  49.             @$file                =      __SITE_FILE_CACHE . "/" . $key .".php"; 
  50.             if(file_exists($file)) 
  51.             { 
  52.                      $data        =    @file_get_contents($file); 
  53.                      $data        =   unserialize($data); 
  54.                      if($data['expire']==0 || $data['expire'] > __SYS_TIME) 
  55.                      { 
  56.                          return $data['content']; 
  57.                      } 
  58.                      else  
  59.                      { 
  60.                          unlink($file); 
  61.                          return array(); 
  62.                      } 
  63.             }         
  64.     } 
  65.      
  66.     /** 
  67.      * 删除缓存文件 
  68.      * @param $key 缓存$key 
  69.      * @return Bool 
  70.      */ 
  71.     public static function delCache($key) 
  72.     {         
  73.         if (@unlink(__SITE_FILE_CACHE."/".$key.".php")) 
  74.         { 
  75.             return true; 
  76.         } 
  77.         else 
  78.         { 
  79.             return false; 
  80.         } 
  81.     } 
  82.      
  83.     /** 
  84.      * 清除所有缓存文件 
  85.      * @return Bool 
  86.      */ 
  87.      
  88.     public static function clearAllCache() 
  89.     { 
  90.         $files = scandir(__SITE_FILE_CACHE); 
  91.         foreach ($files as $val) 
  92.         { 
  93.             @unlink(__SITE_FILE_CACHE."/".$val); 
  94.  
  95.         } 
  96.     } 
  97.      
  98.     /** 
  99.      * 出错处理函数 
  100.      * @param $line 行数 
  101.      * @param $msg  信息 
  102.      */ 
  103.     public static function error($line,$msg) 
  104.     { 
  105.         die("出错文件:".__file__."/n出错行:$line/n错误信息:$msg"); 
  106.     } 
  107. } 
  108.  
  109. ?> 
  110.  

出处:http://www.phpfensi.com/php/20140729/4027.html


相关教程