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