Quick Search:

Mode

Context

Displaying 3 lines of context. None | Less | More | Full

Other Diffs

Ignore

Blank Lines Whitespace:

Diff

1498
 
1860
 
1860
 
cache.php
_> 11 <?php
  22 /*
<> 3 -  $Id: cache.php 1498 2007-03-29 14:04:50Z hpdl $
   3+  $Id: cache.php 1860 2009-03-06 23:25:01Z hpdl $
44 
  55   osCommerce, Open Source E-Commerce Solutions
  66   http://www.oscommerce.com
  77 
<> 8 -  Copyright (c) 2004 osCommerce
   8+  Copyright (c) 2007 osCommerce
99 
  1010   This program is free software; you can redistribute it and/or modify
  1111   it under the terms of the GNU General Public License v2 (1991)
  1212   as published by the Free Software Foundation.
<>  13+*/
1314 
<> 14 -  Class usage examples:
   15+/**
   16+ * The osC_Cache class handles the caching of dynamically generated data
   17+ */
1518 
<> 16 -  - Caching HTML:
  17 -    if ($osC_Cache->read('key', 60) === false) {
  18 -      $osC_Cache->startBuffer();
  19 -      ------ PHP/HTML LOGIC HERE ------
  20 -      $osC_Cache->stopBuffer();
  21 -    }
   19+  class osC_Cache {
2220 
<> 23 -    echo $osC_Cache->getCache();
   21+/**
   22+ * The cached data
   23+ *
   24+ * @var mixed
   25+ * @access private
   26+ */
2427 
<> 25 -  - Caching data (in memory):
  26 -    if ($osC_Cache->read('key', 60) {
  27 -      $variable = $osC_Cache->getCache();
  28 -    } else {
  29 -      $variable = array('some', 'data');
   28+    private $_data;
3029 
<> 31 -      $osC_Cache->writeBuffer($variable);
  32 -    }
  33 -*/
   30+/**
   31+ * The key ID for the cached data
   32+ *
   33+ * @var string
   34+ * @access private
   35+ */
3436 
<> 35 -  class osC_Cache {
  36 -    var $cached_data,
  37 -        $cache_key;
   37+    private $_key;
3838 
<> 39 -    function write($key, &$data) {
  40 -      $filename = DIR_FS_WORK . $key . '.cache';
   39+/**
   40+ * Write the data to a cache file
   41+ *
   42+ * @param string mixed $data The data to cache
   43+ * @param string $key The key ID to save the cached data with
   44+ * @access public
   45+ */
4146 
<> 42 -      if ($fp = @fopen($filename, 'w')) {
  43 -        flock($fp, 2); // LOCK_EX
  44 -        fputs($fp, serialize($data));
  45 -        flock($fp, 3); // LOCK_UN
  46 -        fclose($fp);
  47 -
  48 -        return true;
   47+    public function write($data, $key = null) {
   48+      if ( empty($key) ) {
   49+        $key = $this->_key;
4950       }
  5051 
<> 51 -      return false;
   52+      return ( file_put_contents(DIR_FS_WORK . $key . '.cache', serialize($data), LOCK_EX) !== false );
5253     }
  5354 
<> 54 -    function read($key, $expire = 0) {
  55 -      $this->cache_key = $key;
   55+/**
   56+ * Read data from a cache file if it has not yet expired
   57+ *
   58+ * @param string $key The key ID to read the data from the cached file
   59+ * @param int $expire The amount of minutes the cached data is active for
   60+ * @access public
   61+ * @return boolean
   62+ */
5663 
<>  64+    public function read($key, $expire = null) {
   65+      $this->_key = $key;
   66+
5767       $filename = DIR_FS_WORK . $key . '.cache';
  5868 
<> 59 -      if (file_exists($filename)) {
   69+      if ( file_exists($filename) ) {
6070         $difference = floor((time() - filemtime($filename)) / 60);
  6171 
<> 62 -        if ( ($expire == '0') || ($difference < $expire) ) {
  63 -          if ($fp = @fopen($filename, 'r')) {
  64 -            $this->cached_data = unserialize(fread($fp, filesize($filename)));
   72+        if ( empty($expire) || ( is_numeric($expire) && ($difference < $expire)) ) {
   73+          $this->_data = unserialize(file_get_contents($filename));
6574 
<> 66 -            fclose($fp);
  67 -
  68 -            return true;
  69 -          }
   75+          return true;
7076         }
  7177       }
  7278 
  7379       return false;
  7480     }
  7581 
<> 76 -    function &getCache() {
  77 -      return $this->cached_data;
   82+/**
   83+ * Return the cached data
   84+ *
   85+ * @access public
   86+ * @return mixed
   87+ */
   88+
   89+    public function getCache() {
   90+      return $this->_data;
7891     }
  7992 
<> 80 -    function startBuffer() {
   93+/**
   94+ * Start the buffer to cache its contents
   95+ *
   96+ * @access public
   97+ */
   98+
   99+    public function startBuffer() {
81100       ob_start();
  82101     }
  83102 
<> 84 -    function stopBuffer() {
  85 -      $this->cached_data = ob_get_contents();
   103+/**
   104+ * Stop the buffer and cache its contents
   105+ *
   106+ * @access public
   107+ */
86108 
<>  109+    public function stopBuffer() {
   110+      $this->_data = ob_get_contents();
   111+
87112       ob_end_clean();
  88113 
<> 89 -      $this->write($this->cache_key, $this->cached_data);
   114+      $this->write($this->_data);
90115     }
  91116 
<> 92 -    function writeBuffer(&$data) {
  93 -      $this->cached_data = $data;
   117+/**
   118+ * Delete cached files by their key ID
   119+ *
   120+ * @param string $key The key ID of the cached files to delete
   121+ * @access public
   122+ */
94123 
<> 95 -      $this->write($this->cache_key, $this->cached_data);
  96 -    }
  97 -
  98 -    function clear($key) {
   124+    public static function clear($key) {
99125       $key_length = strlen($key);
  100126 
  101127       $d = dir(DIR_FS_WORK);
  102128 
<> 103 -      while ($entry = $d->read()) {
  104 -        if ((strlen($entry) >= $key_length) && (substr($entry, 0, $key_length) == $key)) {
   129+      while ( ($entry = $d->read()) !== false ) {
   130+        if ( (strlen($entry) >= $key_length) && (substr($entry, 0, $key_length) == $key) ) {
<_ 105131           @unlink(DIR_FS_WORK . $entry);
  106132         }
  107133       }