Nov 10

Content cache class 不指定

shuguang , 16:32 , Cache , 评论(0) , 引用(0) , 阅读(12289) , Via 本站原创 | |
程序名称:Content cache class
开发者/组织名称:Szilágyi Gábor
运行环境:php+mysql,php+文本
官方网站:http://www.sysmarks.com/
演示地址:
文章来源:http://www.OSphp.com.cn
下载地址:
程序介绍:
This class can be used to cache content in files.

It checks whether a given cache file exists. If it exists and is not expired, the class returns the cache file contents. If it has expired, the class removes the cache file.

If the cache file does not exist, the class can create a new cache file with from fresh content.
//OSPHP.COM.Cn开源


The path of the directory where the cache files are stored is configurable.




<?php

/*------------------------------------------------------------------------------------------

CONTENT CACHER CLASS
--------------------
Dev By Leon
2007.10.12.
Budapest

copyright 2007 LeonCreatives - gabor szilagyi
Licenced under the GNU Lesser General Public License (Version 3)
http://www.gnu.org/licenses/lgpl.html

Usage:
------
$cache = new contentCache($_SERVER);
$cache->makeExpiration();
if ($cache->status==true) {
  echo $cache->content;
}
else {
  //Create any content to cache!
  $html="<h1>foo</h1>";
  $cache->makeCache($html);
  echo $html;
}

------------------------------------------------------------------------------------------*/ //开源代码OSPhP.COm.CN


//Class definition
class contentCache {
  
  //Url witch contains information about requested page
  public $url = "";

  //Method type of request
  public $method="";

  //Path to cache files
  public $path="cache/";

  //Cache contents
  public $content="";

  //Result status of cached contents
  public $status=false;
  
  //Data array to store the $_SERVER assoc.
  private $DATA = null;

  //Prepared filename of the cache file
  private $fname="";

  //Last modification timestamp of the cache file
  private $last_mod_time=0;

  //Timestamp of now
  private $now = 0;

  //Used for to calculate the interval of cache time //开源代码OSPhP.COm.CN
  private $exp_min = 0;

  //Expiration time limit
  private $exp_max = 3600;
  
  
  //Constructor and main method
  public function contentCache($data) {
    if (isset($data)) {
      $this->DATA=$data;
      $this->prepareData();
      $result = $this->checkContent();
    }
    else {
      throw new Exception("One of the requested parameters is empty...");
    }
  }
  
  
  private function prepareData() {
    if ($this->DATA['REQUEST_URI']) {
      $this->url=$this->DATA['REQUEST_URI']; //OSPHP.com.CN
      $this->url=str_replace("/","_",$this->url);
      $this->url=str_replace("?","_",$this->url);
      $this->url=str_replace("=","_",$this->url);
      $this->url=str_replace("&","_",$this->url);
      
      if ($this->url!="") {
        $this->fname=$this->url.".cache";
      }
    }
  }
  
  
  private function checkContent() {
    if ($this->fname && is_file($this->path.$this->fname)) {
      $this->content = file_get_contents($this->path.$this->fname);
//oSPHP.COM.CN

      $this->status=true;
    }
    else {
      $this->content="";
      $this->status=false;
    }
  }
  
  
  public function makeCache($content="") {
    if ($content) {
      file_put_contents($this->path.$this->fname,$content);
    }
    else {
      throw new Exception("No content to cache...");
    }
    
    return true;
  }
  
  
  public function makeExpiration() {
    $this->last_mod_time = filemtime($this->path.$this->fname); //开源代码OSPhP.COm.CN
    $this->now = time();
    $this->exp_min=$this->now-$this->last_mod_time;
    
    if ($this->exp_min>$this->exp_max) {
      unlink($this->path.$this->fname);
    }
    
    return true;
  }


//End of the cache class

}


?>
收藏本文到网摘: 添加到“Google书签” 添加到“Yahoo收藏” 添加到“QQ书签” 搜狐网摘 添加到“百度搜藏” 添加到“新浪ViVi收藏夹” 添加到“Del.icio.us” 添加到“365天天网摘” 添加到“天极网摘” 我摘·网摘·网络书签 添加到“POCO网摘” YouNote网摘 添加到“和讯网摘” 添加到“Bolaa博客收录中心” 添加到“igooi网摘” 添加到“天下图摘”
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • 中查看更多“Content cache class”相关内容
  • Tags: , ,
    发表评论

    昵称

    网址

    电邮

    打开HTML 打开UBB 打开表情 隐藏 记住我 [登录系统] [注册]