PHP常用缓存技术详解(一)

(有标题党嫌疑)

正在为173123.cn设计一套缓存机制,将讲所用到的知识在这里总结一下:

在我目前看来,PHP目前常用的缓存技术分为三类,大致上通过缓存数据或者代码,减少了数据库查询次数或者资源初始化次数。
第一类是Smarty等模板引擎生成静态页时采用的ob系列函数(ob_start()、ob_get_contents、flush()等),将页面要缓存的部分通过缓存在缓冲区(*通常还要进一步进行serialize之后保存在文本文件中,每个页面以文件名为$_SERVER['SCRIPT_URI']的独立文件区分),其实现和使用方法大致如下:

require ('cache.class.php');
$cache = new cache('cache/cache_name.php',60*30);  //可自定义缓存文件名和过期时间
$cache->start();   //开始加载缓存  如果存在缓存并且还未国企 则自动include缓存内容
# 你的页面内容
$cache->end();
class cache {
     public $_file;
     public $cache_time;   

     function cache($_file='_index.htm',$cache_time=1) {
         $this->_file        = $_file;
         $this->cache_time    = $cache_time;
     }   

     /*
     * Start cache method without Return
     */
     function start() {   

         if($this->cache_is_active()) {
             include($this->_file);
             exit;
             }
           ob_start();
     }   

     /*
     * End of cache method without Return
     */
     function end() {
         $this->make_cache();
         ob_end_flush();
     }   

     /*
     * Check if cache file is actived
     * Return true/false
     */
     function cache_is_active() {
         if ($this->cache_is_exist()) {
             if (time() - $this->lastModified() < $this->cache_time)
                 Return true;
             else {
                 Return false;
             }
         }
         else {
             Return false;
         }
     }   

     /*
     * Create cache file
     * Return true/false
     */
     function make_cache() {
         $content    = $this->get_cache_content();
         if($this->write_file($content)) {
             Return true;
         }
         else {
             Return false;
         }
     }   

     /*
     * Check if cache file is exists
     * Return true/false
     */
     function cache_is_exist() {
         if(file_exists($this->_file)) {
             Return true;
         }
         else {
             Return false;
         }
     }   

     /*
     * Return last Modified time in bollin formart
     * Usage: $lastmodified = $this->lastModified();
     */
     function lastModified() {
         Return @filemtime($this->_file);
     }   

     /*
     * Return Content of Page
     * Usage: $content = $this->get_cache_content();
     */
     function get_cache_content() {
         $contents = ob_get_contents();
 //        Return ''.$contents;
         Return $contents;
     }   

     /*Write content to $this->_file
     * Return true/false
     * Usage: $this->write_file($content);
     **/
     function write_file($content,$mode='w+')
     {
         $this->mk_dir($this->_file);
         if (!$fp = @fopen($this->_file,$mode)) {
             $this->report_Error($this->_file." 目录或者文件属性无法写入.");
             Return false;
         } else{
             @fwrite($fp,$content);
             @fclose($fp);
             @umask($oldmask);
             Return true;
         }
     }   

     /*
     * Make given dir included in $this->_file
     * Without Return
     * Usage: $this->mk_dir();
     */
     function mk_dir()
     {    //$this->_file    = str_replace('','/');
         $dir    = @explode("/", $this->_file);
         $num    = @count($dir)-1;
         $tmp    = './';
         for($i=0; $i<$num; $i++){
             $tmp    .= $dir[$i];
             if(!file_exists($tmp)){
                 @mkdir($tmp);
                 @chmod($tmp, 0777);
             }
             $tmp    .= '/';
         }
     }   

     /*
     * Unlink an exists cache
     * Return true/false
     * Usage: $this->clear_cache();
     */
     function clear_cache() {
         if (!@unlink($this->_file)) {
             $this->report_Error('Unable to remove cache');
             Return false;
         }
         else {
             Return true;
         }
     }   

     /*
     * Report Error Messages
     * Usage: $this->report_Error($message);
     */
     function report_Error($message=NULL) {
         if($message!=NULL) {
             trigger_error($message);
         }
     }
 }

0 Responses to “PHP常用缓存技术详解(一)”


  • No Comments

Leave a Reply