PHP原生:分享一個輕量級的緩存類=>cache.php


適用:原生PHP

cache.php

tips:代碼最后有適用Demo哦。

<?php
/*
* 緩存類 cache
*/
define("cacheRoot","./cache/");
define("time",0);
define("name","");
define("cacheFileExt","php");
class cache {
//緩存目錄
var $cacheRoot = "./cache/";
//緩存更新時間秒數,0為不緩存
var $cacheLimitTime = 0;
//緩存文件名
var $cacheFileName = "";
//緩存擴展名
var $cacheFileExt = "php";
/*
  * 構造函數,文件名、有效期
  * int $time 緩存更新時間
  */
function cache($name,$time ) {
  if( intval( $time ) )
  $this->cacheLimitTime = $time;
  //$this->cacheLimitTime = $cacheLimitTime;
  $this->cacheFileName = cacheRoot.$name.".".cacheFileExt;    //如:/cache/order.php
  ob_start();
} 
/*
  * 檢查緩存文件是否在設置更新時間之內
  * 返回:如果在更新時間之內則返回文件內容,反之則返回失敗
  */
function cacheCheck(){
  if( file_exists($this->cacheFileName) ) {            //1、是否存在文件,使用絕對路徑 ./cache/xx
      $cTime = $this->getFileCreateTime($this->cacheFileName);
      if( $cTime + $this->cacheLimitTime > time() ) {   //2、文件是否有效
      echo file_get_contents( $this->cacheFileName );   //輸出文件內容到緩存內存中
      $cacheContent = ob_get_contents();                //讀取緩存內存中的數據
      ob_end_flush();
      
      return $cacheContent;     //輸出文件緩存
      exit;
      }else{    //過期,刪除再新建
         $r = @unlink( $this->cacheFileName );
         return false;
      }
  }
  return false;
}
/*
  * 讀取緩存文件或者輸出靜態:邏輯==》1、第一次緩存文件不存在,先寫緩存,立馬讀緩存;2、讀了之后馬上創建文件file
  * string $staticFileName 靜態文件名(含相對路徑)
  */
function caching( $staticFileName = "" ){
  if( $this->cacheFileName ) {
    $cacheContent = ob_get_contents();
    ob_end_flush();
    if( $staticFileName ) {
        $this->saveFile( $staticFileName, $cacheContent );
    }
    if( $this->cacheLimitTime )
    $this->saveFile( $this->cacheFileName, $cacheContent );
  }
} 
/*
  * 清除緩存文件
  * string $fileName 指定文件名(含函數)或者all(全部)
  * 返回:清除成功返回true,反之返回false
  */
function clearCache( $fileName = "all" ) {
  if( $fileName != "all" ) {
  $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;
  if( file_exists( $fileName ) ) {
  return @unlink( $fileName );
  }else return false;
  }
  if ( is_dir( $this->cacheRoot ) ) {
  if ( $dir = @opendir( $this->cacheRoot ) ) {
  while ( $file = @readdir( $dir ) ) {
  $check = is_dir( $file );
  if ( !$check )
  @unlink( $this->cacheRoot . $file );
  }
  @closedir( $dir );
  return true;
  }else{
  return false;
  }
  }else{
  return false;
  }
}
/*根據當前動態文件生成緩存文件名*/
function getCacheFileName() {
  return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
  //return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
  /* $_SERVER["QUERY_STRING"]  獲取查詢 語句,實例中可知,獲取的是?后面的值
    $_SERVER["REQUEST_URI"]   獲取 http://localhost 后面的值,包括/aaa/index.php?p=222&q=333
    $_SERVER["SCRIPT_NAME"]   獲取當前腳本的路徑,如:index.php
    $_SERVER["PHP_SELF"]      當前正在執行腳本的文件名
   */
}
/*
  * 緩存文件建立時間
  * string $fileName 緩存文件名(含相對路徑)
  * 返回:文件生成時間秒數,文件不存在返回0
  */
function getFileCreateTime( $fileName ) {
  if( ! trim($fileName) ) return 0;
  if( file_exists( $fileName ) ) {
  return intval(filemtime( $fileName ));
  }else return 0;
} 
/*
  * 保存文件
  * string $fileName 文件名(含相對路徑)
  * string $text 文件內容
  * 返回:成功返回ture,失敗返回false
  */
function saveFile($fileName, $text) {
  if( ! $fileName || ! $text ) return false;
  if( $this->makeDir( dirname( $fileName ) ) ) {
  if( $fp = fopen( $fileName, "w" ) ) {
  if( @fwrite( $fp, $text ) ) {
  fclose($fp);
  return true;
  }else {
  fclose($fp);
  return false;
  }
  }
  }
  return false;
}
/*
  * 連續建目錄
  * string $dir 目錄字符串
  * int $mode 權限數字
  * 返回:順利創建或者全部已建返回true,其它方式返回false
  */
function makeDir( $dir, $mode = "0777" ) {
  if( ! $dir ) return 0;
  $dir = str_replace( "\\", "/", $dir );
  $mdir = "";
  foreach( explode( "/", $dir ) as $val ) {
  $mdir .= $val."/";
  if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
  if( ! file_exists( $mdir ) ) {
  if(!@mkdir( $mdir, $mode )){
  return false;
  }
  }
  }
  return true;
}
}
?>

<?php
//使用DEMO
// include( "cache.php" );
// $cache = new cache(fileName,7100);  //實例化對象:在/cache/目錄下,創建一個fileName的文件,有效期7100秒
// $cache->cacheCheck();    //檢查文件1、是否存在 2、是否有效;真=返回文件內容,假=返回false
// echo date("Y-m-d H:i:s");//寫入緩存
// $cache->caching();       //沒有參數將上面echo的時間先讀出來,在保存緩存文件;有參數,輸出參數對應的緩存內容

 2、核心:

緩存的使用就是:

ob_start();   //開啟緩存

echo XXX;

echo YY;

$cacheContent = ob_get_contents();  //獲取上面echo的內容,所有內容組合到一起,成為字符串,如上面結果【XXXYY】
ob_end_flush();     //關閉緩存

最后,saveFile($fileName);保存文件名,參照上面代碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM