[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/framework/template/components/cache/ -> outputcache.inc.php (source)

   1  <?php
   2  //--------------------------------------------------------------------------------
   3  // Copyright 2003 Procata, Inc.
   4  // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html)
   5  //--------------------------------------------------------------------------------
   6  /**
   7  * @package WACT_COMPONENT
   8  * @version $Id: outputcache.inc.php,v 1.6 2004/11/20 18:09:48 jeffmoore Exp $
   9  */
  10  /**
  11  * Include PEAR::Cache_Lite
  12  */
  13  if ( !defined('PEAR_LIBRARY_PATH') ) {
  14      define('PEAR_LIBRARY_PATH', ConfigManager::getOptionAsPath('config', 'pear', 'library_path'));
  15  }
  16  if (!@include_once PEAR_LIBRARY_PATH . 'Cache/Lite.php') {
  17      if (!@include_once WACT_ROOT . PEAR_LIBRARY_PATH . 'Cache/Lite.php') {
  18          RaiseError('runtime', 'LIBRARY_REQUIRED', array(
  19              'library' => 'PEAR::Cache_Lite',
  20              'path' => PEAR_LIBRARY_PATH));
  21      }
  22  }
  23  
  24  //--------------------------------------------------------------------------------
  25  /**
  26  * The block tag can be used to show or hide the contents of the block.
  27  * The BlockComponent provides an API which allows the block to be shown
  28  * or hidden at runtime.
  29  * @see http://wact.sourceforge.net/index.php/OutputCacheComponent
  30  * @see http://pear.php.net/Cache_Lite
  31  * @access public
  32  * @package WACT_COMPONENT
  33  */
  34  class OutputCacheComponent extends DataSourceComponent {
  35      /**
  36      * Whether caching is on or off
  37      * @var int 0 or 1 for enabled / disabled caching
  38      * @access private
  39      */
  40      var $caching;
  41      /**
  42      * Instance of PEAR::Cache_Lite
  43      * @var Cache_Lite
  44      * @access private
  45      */
  46      var $cache;
  47      /**
  48      * Name of compiled template file
  49      * @var string
  50      * @access private
  51      */
  52      var $codefile;
  53      /**
  54      * Name of a DataSource variable which defines seperate cacheable content
  55      * such as the contents of $_GET['page']
  56      * @var string
  57      * @access private
  58      */
  59      var $cacheby='';
  60      /**
  61      * A group by which to identify to the file
  62      * @var string
  63      * @access private
  64      */
  65      var $cachegroup=false;
  66      /**
  67      * Rendered HTML stored here
  68      * @var mixed
  69      * @access private
  70      */
  71      var $output = '';
  72      /**
  73      * Store name of cache directory for error reporting
  74      * @var string
  75      * @access private
  76      */
  77      var $cacheDir = '';
  78      /**
  79      * Constructs the OutputCacheComponent
  80      * @param string name of compiled template file
  81      * @param int number of seconds after which cache file expires
  82      * @param string DataSource variable name defining seperate cacheable content
  83      * @param string cache group - identifies a group of cache files
  84      * @access public
  85      */
  86  	function OutputCacheComponent($codefile,$expires=3600,$cacheby='',$cachegroup=false) {
  87          $this->codefile = $codefile;
  88          $tmp_options = ConfigManager::getSection('config','output_cache');
  89          $options = array();
  90          if ( isset ($tmp_options['caching']) && $tmp_options['caching'] == 0 ) {
  91              $options['caching'] = false;
  92              $this->caching = $tmp_options['caching'];
  93          } else {
  94              $options['caching'] = true;
  95              $this->caching = 1;
  96          }
  97          $options['lifeTime'] = $expires;
  98          if ( isset ($tmp_options['cacheBase']) && isset ($tmp_options['cacheDir']) ) {
  99              $options['cacheDir'] = $tmp_options['cacheBase'].$tmp_options['cacheDir'];
 100          } else {
 101              RaiseError('runtime', 'CACHE_LOCATION', array());
 102          }
 103          $this->cacheDir = $options['cacheDir'];
 104          $availableOptions = '{fileNameProtection}{memoryCaching}{onlyMemoryCaching}{memoryCachingLimit}{fileLocking}{writeControl}{readControl}{readControlType}{pearErrorMode}';
 105          foreach ($tmp_options as $key => $value ) {
 106              if (strpos('>'.$availableOptions, '{'.$key.'}')) {
 107                  $options[$key] = $value;
 108              }
 109          }
 110          $this->cache =& new Cache_Lite($options);
 111          $this->cacheby = $cacheby;
 112          $this->cachegroup = $cachegroup;
 113      }
 114      /**
 115      * Returns the ID used by Cache_Lite to identify the cache file
 116      * @return void
 117      * @access public
 118      */
 119  	function getCacheId() {
 120          if ( $this->get($this->cacheby) ) {
 121              return $this->codefile.$this->get($this->cacheby);
 122          } else {
 123              return $this->codefile;
 124          }
 125      }
 126      /**
 127      * Returns the name of the cache group
 128      * @param string
 129      * @return void
 130      * @access public
 131      */
 132  	function getCacheGroup() {
 133          if ( $this->get($this->cachegroup) ) {
 134              return $this->get($this->cachegroup);
 135          } else {
 136              return 'default';
 137          }
 138      }
 139      /**
 140      * Returns the filename name of the cache file.
 141      * It's potentially "dangerous" as it has to access private parts of
 142      * PEAR::Cache_Lite
 143      * @param string
 144      * @return void
 145      * @access private
 146      */
 147  	function getCacheFileName() {
 148          $this->cache->_setFileName($this->getCacheId(),$this->getCacheGroup());
 149          return $this->cache->_file;
 150      }
 151      /**
 152      * Determine whether template is cached
 153      * @return boolean true means template is cached
 154      * @access public
 155      */
 156  	function isCached() {
 157          if ( $this->caching == 1 ) {
 158              if ( $this->output = $this->cache->get($this->getCacheId(),$this->getCacheGroup()) ) {
 159                  return true;
 160              }
 161          }
 162          return false;
 163      }
 164      /**
 165      * Cache output for this template
 166      * @param string parsed template output
 167      * @return void
 168      * @access protected
 169      */
 170  	function cache($output) {
 171          $this->output = $output;
 172          if ( !$this->cache->save($output,$this->getCacheId(),$this->getCacheGroup()) && $this->caching == 1 ) {
 173              RaiseError('runtime', 'CACHE_WRITE', array('cacheDir' => realpath($this->cacheDir)));
 174          }
 175      }
 176      /**
 177      * Delete this cache file
 178      * @return void
 179      * @access public
 180      */
 181  	function flush() {
 182          $this->cache->remove($this->getCacheId(),$this->getCacheGroup());
 183      }
 184      /**
 185      * Flush all the cache files in this group.
 186      * @param mixed (optional) group name as string or nothing
 187      * @return void
 188      * @access public
 189      */
 190  	function flushGroup() {
 191          $this->cache->clean($this->getCacheGroup());
 192      }
 193      /**
 194      * Returns the time the cache was last modified to help with
 195      * issueing HTTP Client Side Caching headers<br />
 196      * Note: accesses PEAR::Cache_Lite private variable $_file
 197      * @return int
 198      * @access public
 199      */
 200  	function lastModified() {
 201          $file = $this->getCacheFileName();
 202          if ( file_exists($file) ) {
 203              return filemtime($file);
 204          } else {
 205              return time();
 206          }
 207      }
 208      /**
 209      * Returns the output to be displayed
 210      * @return mixed either string or false is template not parsed or cached
 211      * @access public
 212      */
 213  	function render() {
 214          echo $this->output;
 215      }
 216  }
 217  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5