phpbar.de logo

Mailinglisten-Archive

[php] Cache Klasse mit Problemen

[php] Cache Klasse mit Problemen

Swiss4.com webmaster at swiss4.com
Die Dez 23 18:16:32 CET 2003


Hallo Zusammen,

Damit ich dynamische Seiten auf dem Server cachen kann, habe ich folgende
Klasse geschrieben.
Nun kommt es zwischendurch vor, dass die erzeugten Dateien 0 Bytes gross
sind. Wie könnte man dieses Problem beheben?


<?php
/* Aufruf:
$cache = &new cache(120);
if($cache->startCache())
{
    exit;
}
/// page ///
$template->parse("CONTENT_FILE","CONTENT");
$template->parse("SIDEBAR_FILE","SIDEBAR");
$template->pparse(OUT,"INDEX");
/// end page ///
$cache->endCache();
*/

class cache
{
 var $cacheFilename;
 var $uniquePageId;
 var $cacheDir;
 var $maxAge;
 var $cacheIsEnabled;


 function cache($maxAge=30)
 {
  /*
  $maxAge is the number of minutes to allow a cached copy to be used before
  generating a new copy.  set this to -1 to use any available cached copy no
matter how old.
  */
  $this->_checkPermission();

  if($this->cacheIsEnabled)
  {
   $this->maxAge = $maxAge;
   $this->uniquePageId = $_SERVER['REQUEST_URI'];
      $this->cacheDir = 'cached_files/';
      $this->cacheFilename = $this->cacheDir.md5($this->uniquePageId);
      //$this->cacheFilename = $this->cacheDir.$this->uniquePageId;
  }
 }


 function _checkPermission()
 {
  global $_CONFIG;

  //$_CONFIG['enableCaching'] = FALSE;

  if(!isset($_CONFIG['enableCaching']) OR !$_CONFIG['enableCaching'])
  {
            $this->cacheIsEnabled = FALSE;
  }
  else
  {
            $this->cacheIsEnabled = TRUE;
  }
 }

  function startCache()
 {
  if($this->cacheIsEnabled)
  {
   if(is_file($this->cacheFilename))
   {
    if(($this->maxAge == -1) || (filemtime($this->cacheFilename) > (time() -
($this->maxAge * 60))))
    {
     readfile($this->cacheFilename);
     return true;
    }
   }
   else
   {
       ob_start();
   }
  }
 }


 function endCache()
 {
  if($this->cacheIsEnabled)
  {
   $cacheContents = ob_get_contents();
   ob_end_flush();
   $fp = fopen($this->cacheFilename, 'w');
   fwrite($fp, $cacheContents);
   fclose($fp);
  }
 }
}
?>


php::bar PHP Wiki   -   Listenarchive