[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[468] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[468] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[468] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[136] | 24 | * Cache.inc.php |
---|
| 25 | * |
---|
[1] | 26 | * Provides an API for storing a limited amount of data |
---|
| 27 | * intended to have a short lifetime in a user's session. |
---|
| 28 | * |
---|
| 29 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[136] | 30 | * @version 2.1 |
---|
[1] | 31 | * @since 2001 |
---|
| 32 | */ |
---|
[136] | 33 | |
---|
| 34 | class Cache { |
---|
| 35 | |
---|
[468] | 36 | // A place to keep object instances for the singleton pattern. |
---|
[484] | 37 | protected static $instances = array(); |
---|
[468] | 38 | |
---|
[152] | 39 | // Namespace of this instance of Prefs. |
---|
[484] | 40 | protected $_ns; |
---|
[152] | 41 | |
---|
| 42 | // Configuration parameters for this object. |
---|
[484] | 43 | protected $_params = array( |
---|
[468] | 44 | |
---|
| 45 | // Type of cache. Currently only 'session' is supported. |
---|
| 46 | 'type' => 'session', |
---|
| 47 | |
---|
[334] | 48 | // If false nothing will be cached or retrieved. Useful for testing realtime data requests. |
---|
[21] | 49 | 'enabled' => true, |
---|
[152] | 50 | |
---|
| 51 | // The maximum size in bytes of any one variable. |
---|
| 52 | 'item_size_limit' => 4194304, // 4 MB |
---|
[468] | 53 | |
---|
[152] | 54 | // The maximum size in bytes before the cache will begin flushing out old items. |
---|
| 55 | 'stack_size_limit' => 4194304, // 4 MB |
---|
[468] | 56 | |
---|
[152] | 57 | // The minimum items to keep in the cache regardless of item or cache size. |
---|
| 58 | 'min_items' => 5, |
---|
[1] | 59 | ); |
---|
[468] | 60 | |
---|
[152] | 61 | /* |
---|
[468] | 62 | * Constructor. This is publically accessible for compatability with older implementations, |
---|
| 63 | * but the preferred method of instantiation is by use of the singleton pattern: |
---|
| 64 | * $cache =& Cache::getInstance('namespace'); |
---|
| 65 | * $cache->setParam(array('enabled' => true)); |
---|
[152] | 66 | * |
---|
| 67 | * @access public |
---|
| 68 | * @param string $namespace This object will store data under this realm. |
---|
| 69 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 70 | * @version 1.0 |
---|
| 71 | * @since 05 Jun 2006 23:14:21 |
---|
| 72 | */ |
---|
[468] | 73 | public function __construct($namespace='') |
---|
[152] | 74 | { |
---|
[172] | 75 | $app =& App::getInstance(); |
---|
[468] | 76 | |
---|
[154] | 77 | $this->_ns = $namespace; |
---|
[172] | 78 | |
---|
| 79 | if (true !== $app->getParam('enable_session')) { |
---|
[238] | 80 | $app->logMsg('Cache disabled, enable_session is false.', LOG_DEBUG, __FILE__, __LINE__); |
---|
[172] | 81 | $this->setParam(array('enabled' => false)); |
---|
| 82 | } |
---|
[468] | 83 | |
---|
[154] | 84 | if (!isset($_SESSION['_cache'][$this->_ns])) { |
---|
[152] | 85 | $this->clear(); |
---|
| 86 | } |
---|
| 87 | } |
---|
[1] | 88 | |
---|
| 89 | /** |
---|
| 90 | * This method enforces the singleton pattern for this class. |
---|
| 91 | * |
---|
[136] | 92 | * @return object Reference to the global Cache object. |
---|
[1] | 93 | * @access public |
---|
| 94 | * @static |
---|
| 95 | */ |
---|
[468] | 96 | public static function &getInstance($namespace='') |
---|
[136] | 97 | { |
---|
[468] | 98 | if (!array_key_exists($namespace, self::$instances)) { |
---|
| 99 | self::$instances[$namespace] = new self($namespace); |
---|
[1] | 100 | } |
---|
[468] | 101 | return self::$instances[$namespace]; |
---|
[1] | 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 106 | * |
---|
| 107 | * @access public |
---|
| 108 | * @param array $params Array of parameters (key => val pairs). |
---|
| 109 | */ |
---|
[468] | 110 | public function setParam($params) |
---|
[1] | 111 | { |
---|
[479] | 112 | $app =& App::getInstance(); |
---|
[21] | 113 | |
---|
[1] | 114 | if (isset($params) && is_array($params)) { |
---|
| 115 | // Merge new parameters with old overriding only those passed. |
---|
[136] | 116 | $this->_params = array_merge($this->_params, $params); |
---|
[1] | 117 | } else { |
---|
[136] | 118 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Return the value of a parameter, if it exists. |
---|
| 124 | * |
---|
| 125 | * @access public |
---|
| 126 | * @param string $param Which parameter to return. |
---|
| 127 | * @return mixed Configured parameter value. |
---|
| 128 | */ |
---|
[468] | 129 | public function getParam($param) |
---|
[1] | 130 | { |
---|
[479] | 131 | $app =& App::getInstance(); |
---|
[468] | 132 | |
---|
[478] | 133 | if (array_key_exists($param, $this->_params)) { |
---|
[136] | 134 | return $this->_params[$param]; |
---|
[1] | 135 | } else { |
---|
[146] | 136 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 137 | return null; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
[334] | 142 | * Stores a new variable in the session cache. The $key should not be numeric |
---|
[468] | 143 | * because the array_shift function will reset the key to the next largest |
---|
[174] | 144 | * int key. Weird behavior I can't understand. For example $cache["123"] will become $cache[0] |
---|
[1] | 145 | * |
---|
[468] | 146 | * @param str $key An identifier for the cached object. |
---|
| 147 | * @param mixed $var The data to store in the session cache. |
---|
| 148 | * @param bool $allow_oversized If we have something really big that we still want to cache, setting this to true allows this. |
---|
| 149 | * @return bool True on success, false otherwise. |
---|
[1] | 150 | */ |
---|
[468] | 151 | public function set($key, $var, $allow_oversized=false) |
---|
[1] | 152 | { |
---|
[479] | 153 | $app =& App::getInstance(); |
---|
[1] | 154 | |
---|
[152] | 155 | if (true !== $this->getParam('enabled')) { |
---|
[316] | 156 | $app->logMsg(sprintf('Cache disabled, not saving data.', null), LOG_DEBUG, __FILE__, __LINE__); |
---|
[21] | 157 | return false; |
---|
| 158 | } |
---|
| 159 | |
---|
[468] | 160 | if (is_numeric($key)) { |
---|
| 161 | $app->logMsg(sprintf('Cache::set key value should not be numeric (%s given)', $key), LOG_WARNING, __FILE__, __LINE__); |
---|
| 162 | } |
---|
| 163 | |
---|
[152] | 164 | $var = serialize($var); |
---|
[247] | 165 | $var_len = mb_strlen($var); |
---|
[42] | 166 | |
---|
[152] | 167 | if ($var_len >= $this->getParam('item_size_limit')) { |
---|
| 168 | $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__); |
---|
[1] | 169 | return false; |
---|
| 170 | } |
---|
[42] | 171 | |
---|
[468] | 172 | if ($allow_oversized && $var_len >= $this->getParam('stack_size_limit')) { |
---|
[152] | 173 | $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__); |
---|
[1] | 174 | return false; |
---|
[468] | 175 | } |
---|
[1] | 176 | |
---|
[152] | 177 | // Remove any value already stored under this key. |
---|
[174] | 178 | unset($_SESSION['_cache'][$this->_ns][$key]); |
---|
[152] | 179 | |
---|
| 180 | // Continue to prune the cache if its size is greater than stack_size_limit, but keep at least min_items. |
---|
[247] | 181 | while (mb_strlen(serialize($_SESSION['_cache'][$this->_ns])) + $var_len >= $this->getParam('stack_size_limit') && sizeof($_SESSION['_cache'][$this->_ns]) >= $this->getParam('min_items')) { |
---|
[154] | 182 | array_shift($_SESSION['_cache'][$this->_ns]); |
---|
[1] | 183 | } |
---|
[42] | 184 | |
---|
[152] | 185 | // Save this value under the specified key. |
---|
[174] | 186 | $_SESSION['_cache'][$this->_ns][$key] =& $var; |
---|
[152] | 187 | |
---|
| 188 | if ($var_len >= 1024000) { |
---|
| 189 | $app->logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $var_len), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 190 | } |
---|
[42] | 191 | |
---|
[136] | 192 | return true; |
---|
[1] | 193 | } |
---|
[42] | 194 | |
---|
[1] | 195 | /** |
---|
[334] | 196 | * Retrieves an object from the session cache and returns it unserialized. |
---|
[1] | 197 | * It also moves it to the top of the stack, which makes it such that the |
---|
| 198 | * cache flushing mechanism of putCache deletes the oldest referenced items |
---|
| 199 | * first. |
---|
| 200 | * |
---|
[136] | 201 | * @param string $key The key for the datum to retrieve. |
---|
[1] | 202 | * @return mixed The requested datum, or false on failure. |
---|
| 203 | */ |
---|
[468] | 204 | public function get($key) |
---|
[1] | 205 | { |
---|
[153] | 206 | $app =& App::getInstance(); |
---|
[162] | 207 | |
---|
[152] | 208 | if (true !== $this->getParam('enabled')) { |
---|
[316] | 209 | $app->logMsg(sprintf('Cache disabled, not getting data.', null), LOG_DEBUG, __FILE__, __LINE__); |
---|
[21] | 210 | return false; |
---|
| 211 | } |
---|
[42] | 212 | |
---|
[480] | 213 | if (isset($_SESSION['_cache'][$this->_ns]) && array_key_exists($key, $_SESSION['_cache'][$this->_ns])) { |
---|
[153] | 214 | $app->logMsg(sprintf('Retreiving %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 215 | // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this? |
---|
[174] | 216 | $tmp =& $_SESSION['_cache'][$this->_ns][$key]; |
---|
| 217 | unset($_SESSION['_cache'][$this->_ns][$key]); |
---|
| 218 | $_SESSION['_cache'][$this->_ns][$key] =& $tmp; |
---|
[1] | 219 | // Return the unserialized datum. |
---|
[174] | 220 | return unserialize($_SESSION['_cache'][$this->_ns][$key]); |
---|
[1] | 221 | } else { |
---|
[172] | 222 | $app->logMsg(sprintf('Missing %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 223 | return false; |
---|
| 224 | } |
---|
| 225 | } |
---|
[42] | 226 | |
---|
[1] | 227 | /** |
---|
| 228 | * Tells you if the object is cached. |
---|
| 229 | * |
---|
[136] | 230 | * @param string $key The key of the object to check. |
---|
[218] | 231 | * @return bool True if a value exists for the given key. |
---|
[1] | 232 | */ |
---|
[468] | 233 | public function exists($key) |
---|
[1] | 234 | { |
---|
[405] | 235 | $app =& App::getInstance(); |
---|
| 236 | |
---|
[152] | 237 | if (true !== $this->getParam('enabled')) { |
---|
[405] | 238 | $app->logMsg(sprintf('Cache disabled on exist assertion.', null), LOG_DEBUG, __FILE__, __LINE__); |
---|
[21] | 239 | return false; |
---|
| 240 | } |
---|
| 241 | |
---|
[480] | 242 | return (isset($_SESSION['_cache'][$this->_ns]) && array_key_exists($key, $_SESSION['_cache'][$this->_ns])); |
---|
[1] | 243 | } |
---|
[42] | 244 | |
---|
[1] | 245 | /** |
---|
[188] | 246 | * Removes a cached object. |
---|
[1] | 247 | * |
---|
[136] | 248 | * @param string $key The key of the object to check. |
---|
[218] | 249 | * @return bool True if the value existed before being unset. |
---|
[1] | 250 | */ |
---|
[468] | 251 | public function delete($key) |
---|
[1] | 252 | { |
---|
[480] | 253 | if (isset($_SESSION['_cache'][$this->_ns]) && array_key_exists($key, $_SESSION['_cache'][$this->_ns])) { |
---|
[218] | 254 | unset($_SESSION['_cache'][$this->_ns][$key]); |
---|
| 255 | return true; |
---|
| 256 | } else { |
---|
| 257 | return false; |
---|
| 258 | } |
---|
[1] | 259 | } |
---|
[468] | 260 | |
---|
[152] | 261 | /* |
---|
| 262 | * Delete all existing items from the cache. |
---|
| 263 | * |
---|
| 264 | * @access public |
---|
| 265 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 266 | * @version 1.0 |
---|
| 267 | * @since 05 Jun 2006 23:51:34 |
---|
| 268 | */ |
---|
[468] | 269 | public function clear() |
---|
[152] | 270 | { |
---|
[154] | 271 | $_SESSION['_cache'][$this->_ns] = array(); |
---|
[152] | 272 | } |
---|
[1] | 273 | |
---|
[136] | 274 | // END Cache |
---|
[1] | 275 | } |
---|
| 276 | |
---|