[ PHPXref.com ] [ Generated: Sun Jul 20 20:58:52 2008 ] [ WackoWiki 4.2 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/classes/ -> cache.php (source)

   1  <?php
   2  // $Id: cache.php 452 2005-02-23 16:31:53Z nikolay $

   3  
   4  class Cache
   5  {
   6   var $cache_ttl = 600;
   7   var $cache_dir = "_cache/";
   8   var $debug = 1;
   9  
  10   //Constructor

  11   function Cache($cache_dir, $cache_ttl)
  12   {
  13    $this->cache_dir = $cache_dir;
  14    $this->cache_ttl = $cache_ttl;
  15    $this->timer = $this->GetMicroTime(); 
  16   }
  17  
  18   //Get content from cache

  19   function GetCached($page, $method, $query)
  20   {
  21    $filename = $this->ConstructID($page, $method, $query);
  22    if (!@file_exists($filename))
  23      return false;
  24    if ((time()-@filemtime($filename)) > $this->cache_ttl)
  25      return false;
  26    $fp = fopen ($filename, "r");
  27    $contents = fread ($fp, filesize ($filename));
  28    fclose ($fp); 
  29    return $contents;
  30   }
  31  
  32   function ConstructID($page, $method, $query)
  33   {
  34    $page = strtolower(str_replace("\\", "", str_replace("'", "", str_replace("_", "", $page))));
  35    $this->Log("ConstructID page=".$page);
  36    $this->Log("ConstructID md5=".md5($page));
  37    $filename = $this->cache_dir.md5($page)."_".$method."_".$query;
  38    return $filename;
  39   }
  40  
  41   //Get timestamp of content from cache

  42   function GetCachedTime($page, $method, $query)
  43   {
  44    $filename = $this->ConstructID($page, $method, $query);
  45    if (!@file_exists($filename))
  46      return false;
  47    
  48    if ((time()-@filemtime($filename)) > $this->cache_ttl)
  49      return false;
  50    
  51    return @filemtime($filename);
  52   }
  53  
  54   //Store content to cache

  55   function StoreToCache($data, $page=false, $method=false, $query=false)
  56   {
  57    if (!$page)   $page   = $this->page;
  58    if (!$method) $method = $this->method;
  59    if (!$query)  $query  = $this->query;
  60    $page = strtolower(str_replace("\\", "", str_replace("'", "", str_replace("_", "", $page))));
  61    $filename = $this->ConstructID($page, $method, $query);
  62    $fp = fopen ($filename, "w");
  63    fputs ($fp, $data);
  64    fclose ($fp); 
  65    if ($this->wacko) $this->wacko->Query("insert into ".$this->wacko->config["table_prefix"]."cache set ".
  66     "name  ='".quote(md5($page))."', ".
  67     "method='".quote($method)."', ".
  68     "query ='".quote($query)."'");
  69    @chmod($newname, octdec('0777'));
  70    return true;
  71   }
  72  
  73   //Invalidate the cache

  74   function CacheInvalidate($page)
  75   {
  76     if ($this->wacko) 
  77     {
  78       $page = strtolower(str_replace("\\", "", str_replace("'", "", str_replace("_", "", $page))));
  79       $this->Log("CacheInvalidate page=".$page);
  80       $this->Log("CacheInvalidate query="."select * from ".$this->wacko->config["table_prefix"]."cache where name ='".quote(md5($page))."'");
  81       $params = $this->wacko->LoadAll("select * from ".$this->wacko->config["table_prefix"]."cache where name ='".quote(md5($page))."'");
  82       $this->Log("CacheInvalidate count params=".count($params));
  83       foreach ($params as $param)
  84       {
  85         $filename = $this->ConstructID($page, $param["method"], $param["query"]);
  86         $this->Log("CacheInvalidate delete=".$filename);
  87         if (@file_exists($filename))
  88           @unlink($filename);
  89       }
  90       $this->wacko->Query("delete from ".$this->wacko->config["table_prefix"]."cache where name ='".quote(md5($page))."'");
  91       $this->Log("CacheInvalidate end");
  92       return true;
  93     }
  94     return false;
  95   }
  96  
  97   function Log($msg)
  98   {
  99    if ($this->debug>1)
 100    {
 101     $fp = fopen ($this->cache_dir."log", "a");
 102     fputs ($fp, $msg."\n");
 103     fclose ($fp); 
 104    }
 105   }
 106  
 107   //Check http-request. May be, output cached version.

 108   function CheckHttpRequest($page, $method)
 109   {
 110  
 111     if (!$page) return false;
 112  
 113     foreach ($_GET as $k => $v)
 114     {
 115      if ($k!="v" && $k!="wakka") $_query[$k]=$v;
 116     }
 117     if ($_query)
 118     {
 119      ksort($_query);
 120      reset($_query);
 121      foreach($_query as $k => $v)
 122      {
 123       $query.=urlencode($k)."=".urlencode($v)."&";
 124      }
 125     }
 126     $this->Log("CheckHttpRequest query=".$query);
 127  
 128     //check cache

 129     if ($mtime = $this->GetCachedTime($page, $method, $query))
 130     {
 131  
 132     $this->Log("CheckHttpRequest incache mtime=".$mtime);
 133     
 134       $gmt = gmdate('D, d M Y H:i:s \G\M\T', $mtime);
 135       $etag = $_SERVER["HTTP_IF_NONE_MATCH"];
 136       $lastm = $_SERVER["HTTP_IF_MODIFIED_SINCE"];
 137  
 138       if ($p = strpos($lastm,";")) $lastm=substr($lastm,0,$p);
 139  
 140       if ($_SERVER["REQUEST_METHOD"]=="GET") //may be we need HEAD support ???
 141       {
 142         if (!$lastm && !$etag);
 143         else
 144         if ($lastm && $gmt!=$lastm);
 145         else
 146         if ($etag && $gmt!=trim($etag, '\"'));
 147         else
 148         {
 149           header ("HTTP/1.1 304 Not Modified");
 150           die();
 151         }
 152  
 153         $cached = $this->GetCached($page, $method, $query);
 154         header ("Last-Modified: ".$gmt);
 155         header ("ETag: \"".$gmt."\"");
 156         //header ("Content-Type: text/xml");

 157         //header ("Content-Length: ".strlen($cached));

 158         //header ("Cache-Control: max-age=0");

 159         //header ("Expires: ".gmdate('D, d M Y H:i:s \G\M\T', time()));

 160         echo ($cached);
 161  
 162         // how much time script take

 163         if ($this->debug>=1 && strpos($method,".xml")===false) 
 164         {
 165          $ddd = $this->GetMicroTime();
 166          echo ("<div style='margin:5px 20px; color:#999999'><small>cache time: ".(number_format(($ddd-$this->timer),3))." s<br />");
 167          echo "</small></div>";
 168         }
 169         if (strpos($method,".xml")===false) 
 170           echo "</body></html>";
 171  
 172         die();
 173       }
 174     }
 175     
 176     //We have no valid cached page

 177     $this->page = $page;
 178     $this->method = $method;
 179     $this->query = $query;
 180     return true;
 181   }
 182  
 183   function Output()
 184   {
 185     clearstatcache();
 186     if (!($mtime = $this->GetCachedTime($this->page, $this->method, $this->query)))
 187       $mtime = time();
 188     {
 189       $gmt = gmdate('D, d M Y H:i:s \G\M\T', $mtime);
 190       $res = &$this->result;
 191       header ("Last-Modified: ".$gmt);
 192       header ("ETag: \"".$gmt."\"");
 193       header ("Content-Type: text/xml");
 194       //header ("Content-Length: ".strlen($res));

 195       //header ("Cache-Control: max-age=0");

 196       //header ("Expires: ".gmdate('D, d M Y H:i:s \G\M\T', time()));

 197       echo ($res);
 198       die();
 199     }
 200   }
 201  
 202   function GetMicroTime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); }
 203  
 204  
 205  }
 206  
 207  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]