php中檔案快取實現程式碼

來源:互聯網
上載者:User

檔案快取原理

1.把需要緩衝的資料通過serialize函數序列化後直接儲存到檔案。在需要使用快取資料的時候,通過還原序列化讀入檔案內容並複製給需要的變數,然後使用。不經常改動的資料可以寫入快取檔案。


檔案快取執行個體

index.php

 代碼如下 複製代碼

define('CACHE_ROOT','./');
 include_once('./cache.func.php');
 $file = 'qzp';
 $cacheFile = getCacheFileByID($file,'info/');
 
 print_R(readCache($cacheFile));
 
 $info = array(
      'username'=>'qzp',
      'area_name'=>'admin',
      'mp_name'=>'1234',
      'gh_name'=>'5678');
writeCache($cacheFile,$info);

cache.func.php檔案

 function arrayeval($array, $level = 0) {
    $space = '';
    for($i = 0; $i <= $level; $i++) {
        $space .= "t";
    }
    $evaluate = "Arrayn$space(n";
    $comma = $space;
    foreach($array as $key => $val) {
        $key = is_string($key) ? '''.addcslashes($key, ''\').''' : $key;
        $val = !is_array($val) && (!preg_match("/^-?[1-9]d*$/", $val) || strlen($val) > 12) ? '''.addcslashes($val, ''\').''' : $val;
        if(is_array($val)) {
            $evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
        } else {
            $evaluate .= "$comma$key => $val";
        }
        $comma = ",n$space";
    }
    $evaluate .= "n$space)";
    return $evaluate;
}
 
function getCacheFileByID($id,$pre='info/',$md5=true){
 if($md5){
  $md5id = md5($id);
  return CACHE_ROOT.'/'.$pre.substr($md5id,0,2).'/'.substr($md5id,2,2).'/'.$id;
 }else{
  return CACHE_ROOT.'/'.$pre.$id;
 }
}
 
function readCache($filename,$time=0){
 if($datas = @file_get_contents($filename)){
  $datas = unserialize($datas);
  if($time < 1 || (time() - $datas['time'] < $time)){
   return $datas['data'];
  }
 }
 return false;
}
 
function writeCache($filename,$data){
 makeDir(dirname($filename));
 if($handle = fopen($filename,'w+')){
  $datas = array('data'=>$data,'time'=>time());
  flock($handle,LOCK_EX);
  $rs = fputs($handle,serialize($datas));
  flock($handle,LOCK_UN);
  fclose($handle);
  if($rs!==false){
   return true;
  }
 }
 return false;
}
 
function makeDir($path)
{
 if (! file_exists ( $path )) {
  makeDir ( dirname ( $path ) );
  if (! mkdir ( $path, 0777 ))
  die ( '無法建立檔案夾' . $path );
 }
}

把要緩衝的檔案序列化下存放起來,當使用的時候還原序列化回來使用。

使用對PHP模板資料進行緩衝的工具smarty。smarty將HTML檔案快取成一個靜態HTML頁,需要緩衝的模板檔案可以使用smarty。
例:

 代碼如下 複製代碼

<?php
//包含Smarty類庫
require('libs/Smarty.class.php');
 
//建立Smarty類的對象
$smarty = new Smarty;
 
//啟用緩衝
$smarty->caching = true;
 
//指定快取檔案儲存的目錄
$smarty->cache_dir = "./cache/";
 
//也會把輸出儲存
$smarty->display('index.tpl')
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.