Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

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

    r119 r136  
    11<?php
    22/**
    3  * SessionCache.inc.php
     3 * Cache.inc.php
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
     5 *
    46 * Provides an API for storing a limited amount of data
    57 * intended to have a short lifetime in a user's session.
    68 *
    7  * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.
    89 * @author  Quinn Comendant <quinn@strangecode.com>
    9  * @version 1.2
     10 * @version 2.1
    1011 * @since   2001
    1112 */
    12 class SessionCache
    13 {
     13 
     14// Flags.
     15define('CACHE_IGNORE_SIZE', 1);
     16
     17class Cache {
     18
    1419    var $_params = array(
    1520        'enabled' => true,
     
    2227     * This method enforces the singleton pattern for this class.
    2328     *
    24      * @return  object  Reference to the global SessionCache object.
     29     * @return  object  Reference to the global Cache object.
    2530     * @access  public
    2631     * @static
    2732     */
    28     function &getInstance() {
     33    function &getInstance()
     34    {
    2935        static $instance = null;
    3036
    3137        if ($instance === null) {
    32             $instance = new SessionCache();
     38            $instance = new Cache();
    3339        }
    3440
     
    4450    function setParam($params)
    4551    {
    46         if (!isset($_this) || !is_a($_this, 'SessionCache') && !is_subclass_of($_this, 'SessionCache')) {
    47             $_this =& SessionCache::getInstance();
    48         }
     52        $app =& App::getInstance();
    4953
    5054        if (isset($params) && is_array($params)) {
    5155            // Merge new parameters with old overriding only those passed.
    52             $_this->_params = array_merge($_this->_params, $params);
    53         } else {
    54             App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
     56            $this->_params = array_merge($this->_params, $params);
     57        } else {
     58            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
    5559        }
    5660    }
     
    6569    function getParam($param)
    6670    {
    67         if (!isset($_this) || !is_a($_this, 'SessionCache') && !is_subclass_of($_this, 'SessionCache')) {
    68             $_this =& SessionCache::getInstance();
    69         }
    70 
    71         if (isset($_this->_params[$param])) {
    72             return $_this->_params[$param];
    73         } else {
    74             App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     71        $app =& App::getInstance();
     72   
     73        if (isset($this->_params[$param])) {
     74            return $this->_params[$param];
     75        } else {
     76            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
    7577            return null;
    7678        }
     
    7880
    7981    /**
    80      * Stores a new variable in the session cache. The $var_id is is md5'ed
     82     * Stores a new variable in the session cache. The $key is is md5'ed
    8183     * because if a variable id is a very large integer, the array_shift function
    82      * will reset the key to the next largest int key. Weird behaviour I can't
     84     * will reset the key to the next largest int key. Weird behavior I can't
    8385     * understand. $session_cache[32341234123] will become $session_cache[0]
    8486     * for example. Usage warning: if the variable is too big to fit, or is
    8587     * old and discarded, you must provide alternative ways of accessing the data.
    8688     *
     89     * @param str   $key        An identifier for the cached object.
    8790     * @param mixed $var          The var to store in the session cache.
    88      * @param str   $var_id       An identifyer for the cached object.
    89      * @param bool  $force_it_in  If we have something really big that we
    90      *                            still want to cache, setting this true
    91      *                            allows this.
    92      *
    93      * @return string        The $var_id, or false if the object was too big to cache.
    94      */
    95     function putCache($var, $var_id, $force_it_in=false)
    96     {
    97         if (!isset($_this) || !is_a($_this, 'SessionCache') && !is_subclass_of($_this, 'SessionCache')) {
    98             $_this =& SessionCache::getInstance();
    99         }
    100 
    101         if (!$_this->getParam('enabled')) {
    102             App::logMsg(sprintf('SessionCache not enabled, not saving data.', null), LOG_DEBUG, __FILE__, __LINE__);
    103             return false;
    104         }
    105 
    106         $var_id = md5($var_id);
     91     * @param bool  $flags      If we have something really big that we
     92     *                            still want to cache, setting this to
     93     *                            CACHE_IGNORE_SIZE allows this.
     94     *
     95     * @return bool               True on success, false otherwise.
     96     */
     97    function set($key, $var, $flags=0)
     98    {
     99        $app =& App::getInstance();
     100
     101        if (!$this->getParam('enabled')) {
     102            $app->logMsg(sprintf('Cache not enabled, not saving data.', null), LOG_DEBUG, __FILE__, __LINE__);
     103            return false;
     104        }
     105
     106        $key = md5($key);
    107107        $serialized_var = serialize($var);
    108108        $serialized_var_len = strlen($serialized_var);
    109109
    110         if ($serialized_var_len >= $_this->getParam('soft_limit') && !$force_it_in) {
    111             App::logMsg(sprintf('Serialized variable (%s bytes) more than soft_limit (%s bytes).', $serialized_var_len, $_this->getParam('soft_limit')), LOG_NOTICE, __FILE__, __LINE__);
    112             return false;
    113         }
    114 
    115         if ($serialized_var_len >= $_this->getParam('hard_limit')) {
    116             App::logMsg(sprintf('Serialized variable (%s bytes) more than hard_limit (%s bytes).', $serialized_var_len, $_this->getParam('hard_limit')), LOG_NOTICE, __FILE__, __LINE__);
     110        if ($flags & CACHE_IGNORE_SIZE > 0 && $serialized_var_len >= $this->getParam('soft_limit')) {
     111            $app->logMsg(sprintf('Serialized variable (%s bytes) more than soft_limit (%s bytes).', $serialized_var_len, $this->getParam('soft_limit')), LOG_NOTICE, __FILE__, __LINE__);
     112            return false;
     113        }
     114
     115        if ($serialized_var_len >= $this->getParam('hard_limit')) {
     116            $app->logMsg(sprintf('Serialized variable (%s bytes) more than hard_limit (%s bytes).', $serialized_var_len, $this->getParam('hard_limit')), LOG_NOTICE, __FILE__, __LINE__);
    117117            return false;
    118118        }
     
    121121            $_SESSION['_session_cache'] = array();
    122122        } else {
    123             unset($_SESSION['_session_cache'][$var_id]);
     123            unset($_SESSION['_session_cache'][$key]);
    124124            // Continue to prune the cache if it's length is too long for the new variable to fit, but keep at least MIN_ITEMS at least.
    125             while (strlen(serialize($_SESSION['_session_cache'])) + $serialized_var_len >= $_this->getParam('soft_limit')
    126             && sizeof($_SESSION['_session_cache']) >= $_this->getParam('min_items')) {
     125            while (strlen(serialize($_SESSION['_session_cache'])) + $serialized_var_len >= $this->getParam('soft_limit')
     126            && sizeof($_SESSION['_session_cache']) >= $this->getParam('min_items')) {
    127127                array_shift($_SESSION['_session_cache']);
    128128            }
    129129        }
    130         $_SESSION['_session_cache'][$var_id] =& $serialized_var;
     130        $_SESSION['_session_cache'][$key] =& $serialized_var;
    131131
    132132        if ($serialized_var_len >= 1024000) {
    133             App::logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $serialized_var_len), LOG_DEBUG, __FILE__, __LINE__);
    134         }
    135 
    136         return $var_id;
     133            $app->logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $serialized_var_len), LOG_DEBUG, __FILE__, __LINE__);
     134        }
     135
     136        return true;
    137137    }
    138138
     
    143143     * first.
    144144     *
    145      * @param string $var_id  The identifyer for the datum to retrieve.
     145     * @param string $key  The key for the datum to retrieve.
    146146     *
    147147     * @return mixed          The requested datum, or false on failure.
    148148     */
    149     function getCache($var_id)
    150     {
    151         if (!isset($_this) || !is_a($_this, 'SessionCache') && !is_subclass_of($_this, 'SessionCache')) {
    152             $_this =& SessionCache::getInstance();
    153         }
    154 
    155         if (!$_this->getParam('enabled')) {
    156             return false;
    157         }
    158 
    159         $var_id = md5($var_id);
    160         if (isset($_SESSION['_session_cache'][$var_id])) {
     149    function get($key)
     150    {
     151        if (!$this->getParam('enabled')) {
     152            return false;
     153        }
     154
     155        $key = md5($key);
     156        if (isset($_SESSION['_session_cache'][$key])) {
    161157            // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this?
    162             $tmp =& $_SESSION['_session_cache'][$var_id];
    163             unset($_SESSION['_session_cache'][$var_id]);
    164             $_SESSION['_session_cache'][$var_id] =& $tmp;
     158            $tmp =& $_SESSION['_session_cache'][$key];
     159            unset($_SESSION['_session_cache'][$key]);
     160            $_SESSION['_session_cache'][$key] =& $tmp;
    165161            // Return the unserialized datum.
    166             return unserialize($_SESSION['_session_cache'][$var_id]);
     162            return unserialize($_SESSION['_session_cache'][$key]);
    167163        } else {
    168164            return false;
     
    173169     * Tells you if the object is cached.
    174170     *
    175      * @param string $var_id  The ID of the object to check.
     171     * @param string $key  The key of the object to check.
    176172     *
    177173     * @return bool           The return from isset().
    178174     */
    179     function isCached($var_id)
    180     {
    181         if (!isset($_this) || !is_a($_this, 'SessionCache') && !is_subclass_of($_this, 'SessionCache')) {
    182             $_this =& SessionCache::getInstance();
    183         }
    184 
    185         if (!$_this->getParam('enabled')) {
    186             return false;
    187         }
    188 
    189         $var_id = md5($var_id);
    190         return isset($_SESSION['_session_cache'][$var_id]);
     175    function exists($key)
     176    {
     177        if (!$this->getParam('enabled')) {
     178            return false;
     179        }
     180
     181        $key = md5($key);
     182        return isset($_SESSION['_session_cache'][$key]);
    191183    }
    192184
     
    194186     * Tells you if the object is cached.
    195187     *
    196      * @param string $var_id  The ID of the object to check.
     188     * @param string $key  The key of the object to check.
    197189     *
    198190     * @return bool           The return from isset().
    199191     */
    200     function breakCache($var_id)
    201     {
    202         $var_id = md5($var_id);
    203         if (isset($_SESSION['_session_cache'][$var_id])) {
    204             unset($_SESSION['_session_cache'][$var_id]);
    205         }
    206     }
    207 
    208 // END SessionCache
     192    function delete($key)
     193    {
     194        $key = md5($key);
     195        if (isset($_SESSION['_session_cache'][$key])) {
     196            unset($_SESSION['_session_cache'][$key]);
     197        }
     198    }
     199
     200// END Cache
    209201}
    210202
Note: See TracChangeset for help on using the changeset viewer.