Ignore:
Timestamp:
Jun 7, 2006 5:35:16 AM (18 years ago)
Author:
scdev
Message:

Q - In the middle of working on the Prefs and Cache instantiation mode...can't decide to use singleton pattern or global vars. Updated ImageThumb? to allow filenames with path elements such as 01/23/4567_file.jpg.

File:
1 edited

Legend:

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

    r146 r152  
    1313 
    1414// Flags.
    15 define('CACHE_IGNORE_SIZE', 1);
     15define('CACHE_ALLOW_OVERSIZED', 1);
    1616
    1717class Cache {
    1818
     19    // Namespace of this instance of Prefs.
     20    var $_ns;
     21
     22    // Configuration parameters for this object.
    1923    var $_params = array(
     24       
     25        // If false nothing will be cached or retreived. Useful for testing realtime data requests.
    2026        'enabled' => true,
    21         'soft_limit' => 204800,
    22         'hard_limit' => 4194304,
    23         'min_items' => 3,
     27
     28        // The maximum size in bytes of any one variable.
     29        'item_size_limit' => 4194304, // 4 MB
     30       
     31        // The maximum size in bytes before the cache will begin flushing out old items.
     32        'stack_size_limit' => 4194304, // 4 MB
     33       
     34        // The minimum items to keep in the cache regardless of item or cache size.
     35        'min_items' => 5,
    2436    );
     37   
     38    /*
     39    * Constructor
     40    *
     41    * @access   public
     42    * @param    string  $namespace  This object will store data under this realm.
     43    * @author   Quinn Comendant <quinn@strangecode.com>
     44    * @version  1.0
     45    * @since    05 Jun 2006 23:14:21
     46    */
     47    function Cache($namespace='')
     48    {
     49        $this->_ns = '_cache' . $namespace;
     50       
     51        if (!isset($_SESSION[$this->_ns])) {
     52            $this->clear();
     53        }
     54    }
    2555
    2656    /**
     
    3161     * @static
    3262     */
    33     function &getInstance()
    34     {
    35         static $instance = null;
    36 
    37         if ($instance === null) {
    38             $instance = new Cache();
    39         }
    40 
    41         return $instance;
     63    function &getInstance($namespace='')
     64    {
     65        static $instances = array();
     66
     67        if (!array_key_exists($namespace, $instances)) {
     68            $instances[$namespace] = new Cache($namespace);
     69        }
     70
     71        return $instances[$namespace];
    4272    }
    4373
     
    81111    /**
    82112     * Stores a new variable in the session cache. The $key is is md5'ed
    83      * because if a variable id is a very large integer, the array_shift function
     113     * because if a key is numeric, the array_shift function
    84114     * will reset the key to the next largest int key. Weird behavior I can't
    85      * understand. $session_cache[32341234123] will become $session_cache[0]
    86      * for example. Usage warning: if the variable is too big to fit, or is
    87      * old and discarded, you must provide alternative ways of accessing the data.
     115     * understand. For example $cache["123"] will become $cache[0]
    88116     *
    89117     * @param str   $key        An identifier for the cached object.
    90      * @param mixed $var          The var to store in the session cache.
     118     * @param mixed $var        The var to store in the session cache.
    91119     * @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.
     120     *                          still want to cache, setting this to
     121     *                          CACHE_ALLOW_OVERSIZED allows this.
     122     * @return bool             True on success, false otherwise.
    96123     */
    97124    function set($key, $var, $flags=0)
     
    99126        $app =& App::getInstance();
    100127
    101         if (!$this->getParam('enabled')) {
     128        if (true !== $this->getParam('enabled')) {
    102129            $app->logMsg(sprintf('Cache not enabled, not saving data.', null), LOG_DEBUG, __FILE__, __LINE__);
    103130            return false;
     
    105132
    106133        $key = md5($key);
    107         $serialized_var = serialize($var);
    108         $serialized_var_len = strlen($serialized_var);
    109 
    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__);
    117             return false;
    118         }
    119 
    120         if (!isset($_SESSION['_session_cache'])) {
    121             $_SESSION['_session_cache'] = array();
    122         } else {
    123             unset($_SESSION['_session_cache'][$key]);
    124             // 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')) {
    127                 array_shift($_SESSION['_session_cache']);
    128             }
    129         }
    130         $_SESSION['_session_cache'][$key] =& $serialized_var;
    131 
    132         if ($serialized_var_len >= 1024000) {
    133             $app->logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $serialized_var_len), LOG_DEBUG, __FILE__, __LINE__);
     134        $var = serialize($var);
     135        $var_len = strlen($var);
     136
     137        if ($var_len >= $this->getParam('item_size_limit')) {
     138            $app->logMsg(sprintf('Serialized variable (%s bytes) more than item_size_limit (%s bytes).', $var_len, $this->getParam('item_size_limit')), LOG_NOTICE, __FILE__, __LINE__);
     139            return false;
     140        }
     141
     142        if ($flags & CACHE_ALLOW_OVERSIZED == 0 && $var_len >= $this->getParam('stack_size_limit')) {
     143            $app->logMsg(sprintf('Serialized variable (%s bytes) more than stack_size_limit (%s bytes).', $var_len, $this->getParam('stack_size_limit')), LOG_NOTICE, __FILE__, __LINE__);
     144            return false;
     145        }       
     146
     147        // Remove any value already stored under this key.
     148        unset($_SESSION[$this->_ns][$key]);
     149
     150        // Continue to prune the cache if its size is greater than stack_size_limit, but keep at least min_items.
     151        while (strlen(serialize($_SESSION[$this->_ns])) + $var_len >= $this->getParam('stack_size_limit') && sizeof($_SESSION[$this->_ns]) >= $this->getParam('min_items')) {
     152            array_shift($_SESSION[$this->_ns]);
     153        }
     154
     155        // Save this value under the specified key.
     156        $_SESSION[$this->_ns][$key] =& $var;
     157
     158        if ($var_len >= 1024000) {
     159            $app->logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $var_len), LOG_DEBUG, __FILE__, __LINE__);
    134160        }
    135161
     
    144170     *
    145171     * @param string $key  The key for the datum to retrieve.
    146      *
    147172     * @return mixed          The requested datum, or false on failure.
    148173     */
    149174    function get($key)
    150175    {
    151         if (!$this->getParam('enabled')) {
    152             return false;
    153         }
    154 
    155         $key = md5($key);
    156         if (isset($_SESSION['_session_cache'][$key])) {
     176        if (true !== $this->getParam('enabled')) {
     177            return false;
     178        }
     179
     180        $key = md5($key);
     181        if (isset($_SESSION[$this->_ns][$key])) {
    157182            // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this?
    158             $tmp =& $_SESSION['_session_cache'][$key];
    159             unset($_SESSION['_session_cache'][$key]);
    160             $_SESSION['_session_cache'][$key] =& $tmp;
     183            $tmp =& $_SESSION[$this->_ns][$key];
     184            unset($_SESSION[$this->_ns][$key]);
     185            $_SESSION[$this->_ns][$key] =& $tmp;
    161186            // Return the unserialized datum.
    162             return unserialize($_SESSION['_session_cache'][$key]);
     187            return unserialize($_SESSION[$this->_ns][$key]);
    163188        } else {
    164189            return false;
     
    170195     *
    171196     * @param string $key  The key of the object to check.
    172      *
    173197     * @return bool           The return from isset().
    174198     */
    175199    function exists($key)
    176200    {
    177         if (!$this->getParam('enabled')) {
    178             return false;
    179         }
    180 
    181         $key = md5($key);
    182         return isset($_SESSION['_session_cache'][$key]);
     201        if (true !== $this->getParam('enabled')) {
     202            return false;
     203        }
     204
     205        $key = md5($key);
     206        return array_key_exists($key, $_SESSION[$this->_ns]);
    183207    }
    184208
     
    187211     *
    188212     * @param string $key  The key of the object to check.
    189      *
    190213     * @return bool           The return from isset().
    191214     */
     
    193216    {
    194217        $key = md5($key);
    195         if (isset($_SESSION['_session_cache'][$key])) {
    196             unset($_SESSION['_session_cache'][$key]);
    197         }
     218        unset($_SESSION[$this->_ns][$key]);
     219    }
     220   
     221    /*
     222    * Delete all existing items from the cache.
     223    *
     224    * @access   public
     225    * @author   Quinn Comendant <quinn@strangecode.com>
     226    * @version  1.0
     227    * @since    05 Jun 2006 23:51:34
     228    */
     229    function clear()
     230    {
     231        $_SESSION[$this->_ns] = array();
    198232    }
    199233
Note: See TracChangeset for help on using the changeset viewer.