Changeset 638


Ignore:
Timestamp:
Sep 29, 2018 6:51:19 PM (6 years ago)
Author:
anonymous
Message:

Add cache auto expires timeout

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Cache.inc.php

    r611 r638  
    6060        // The minimum items to keep in the cache regardless of item or cache size.
    6161        'min_items' => 5,
     62
     63        // How long in seconds before items in the cache are considered expired.
     64        // When the cache expires, Cache->get() returns null.
     65        'expires' => 3600,
    6266    );
    6367
     
    187191
    188192        // Save this value under the specified key.
    189         $_SESSION['_cache'][$this->_ns][$key] =& $var;
     193        $_SESSION['_cache'][$this->_ns][$key] = array(
     194            'var' => $var,
     195            'time' => time(),
     196        );
    190197
    191198        $app->logMsg(sprintf('Set cache item “%s”', $key), LOG_DEBUG, __FILE__, __LINE__);
     
    217224
    218225        if (isset($_SESSION['_cache'][$this->_ns]) && array_key_exists($key, $_SESSION['_cache'][$this->_ns])) {
    219             $app->logMsg(sprintf('Retrieving %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__);
    220             // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this?
    221             $tmp =& $_SESSION['_cache'][$this->_ns][$key];
    222             unset($_SESSION['_cache'][$this->_ns][$key]);
    223             $_SESSION['_cache'][$this->_ns][$key] =& $tmp;
    224             // Return the unserialized datum.
    225             return unserialize($_SESSION['_cache'][$this->_ns][$key]);
     226            if (isset($_SESSION['_cache'][$this->_ns][$key]['time']) && $_SESSION['_cache'][$this->_ns][$key]['time'] > (time() - $this->getParam('expires'))) {
     227                $app->logMsg(sprintf('Retrieving %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__);
     228                // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this?
     229                $tmp =& $_SESSION['_cache'][$this->_ns][$key];
     230                unset($_SESSION['_cache'][$this->_ns][$key]);
     231                $_SESSION['_cache'][$this->_ns][$key] =& $tmp;
     232                // Return the unserialized datum.
     233                return unserialize($_SESSION['_cache'][$this->_ns][$key]['var']);
     234            } else {
     235                // Cached item has expired.
     236                $app->logMsg(sprintf('Cached %s expired %s ago', $key, humanTime(time() - $_SESSION['_cache'][$this->_ns][$key]['time'])), LOG_DEBUG, __FILE__, __LINE__);
     237                return null;
     238            }
    226239        } else {
    227240            $app->logMsg(sprintf('Missing %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__);
     
    245258        }
    246259
    247         return (isset($_SESSION['_cache'][$this->_ns]) && array_key_exists($key, $_SESSION['_cache'][$this->_ns]));
     260        if (isset($_SESSION['_cache'][$this->_ns])
     261        && array_key_exists($key, $_SESSION['_cache'][$this->_ns])
     262        && isset($_SESSION['_cache'][$this->_ns][$key]['time'])
     263        && $_SESSION['_cache'][$this->_ns][$key]['time'] > (time() - $this->getParam('expires'))) {
     264            $app->logMsg(sprintf('Cache item “%s” exists', $key), LOG_DEBUG, __FILE__, __LINE__);
     265            return true;
     266        } else {
     267            $app->logMsg(sprintf('Cache item “%s” missing or expired', $key), LOG_DEBUG, __FILE__, __LINE__);
     268            return false;
     269        }
    248270    }
    249271
Note: See TracChangeset for help on using the changeset viewer.