Changeset 468 for trunk/lib/Cache.inc.php
- Timestamp:
- Feb 20, 2014 3:03:59 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
/branches/eli_branch merged eligible
-
Property
svn:mergeinfo
set to
-
trunk/lib/Cache.inc.php
r405 r468 4 4 * For details visit the project site: <http://trac.strangecode.com/codebase/> 5 5 * Copyright 2001-2012 Strangecode, LLC 6 * 6 * 7 7 * This file is part of The Strangecode Codebase. 8 8 * … … 11 11 * Free Software Foundation, either version 3 of the License, or (at your option) 12 12 * any later version. 13 * 13 * 14 14 * The Strangecode Codebase is distributed in the hope that it will be useful, but 15 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 17 * details. 18 * 18 * 19 19 * You should have received a copy of the GNU General Public License along with 20 20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. … … 31 31 * @since 2001 32 32 */ 33 34 // Flags.35 define('CACHE_ALLOW_OVERSIZED', 1);36 33 37 34 class Cache { 38 35 36 // A place to keep object instances for the singleton pattern. 37 private static $instances = array(); 38 39 39 // Namespace of this instance of Prefs. 40 var$_ns;40 private $_ns; 41 41 42 42 // Configuration parameters for this object. 43 var $_params = array( 44 43 private $_params = array( 44 45 // Type of cache. Currently only 'session' is supported. 46 'type' => 'session', 47 45 48 // If false nothing will be cached or retrieved. Useful for testing realtime data requests. 46 49 'enabled' => true, … … 48 51 // The maximum size in bytes of any one variable. 49 52 'item_size_limit' => 4194304, // 4 MB 50 53 51 54 // The maximum size in bytes before the cache will begin flushing out old items. 52 55 'stack_size_limit' => 4194304, // 4 MB 53 56 54 57 // The minimum items to keep in the cache regardless of item or cache size. 55 58 'min_items' => 5, 56 59 ); 57 60 58 61 /* 59 * Constructor 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)); 60 66 * 61 67 * @access public … … 65 71 * @since 05 Jun 2006 23:14:21 66 72 */ 67 function Cache($namespace='')73 public function __construct($namespace='') 68 74 { 69 75 $app =& App::getInstance(); 70 76 71 77 $this->_ns = $namespace; 72 78 … … 75 81 $this->setParam(array('enabled' => false)); 76 82 } 77 83 78 84 if (!isset($_SESSION['_cache'][$this->_ns])) { 79 85 $this->clear(); … … 88 94 * @static 89 95 */ 90 static function &getInstance($namespace='') 91 { 92 static $instances = array(); 93 94 if (!array_key_exists($namespace, $instances)) { 95 $instances[$namespace] = new Cache($namespace); 96 } 97 98 return $instances[$namespace]; 96 public static function &getInstance($namespace='') 97 { 98 if (!array_key_exists($namespace, self::$instances)) { 99 self::$instances[$namespace] = new self($namespace); 100 } 101 return self::$instances[$namespace]; 99 102 } 100 103 … … 105 108 * @param array $params Array of parameters (key => val pairs). 106 109 */ 107 function setParam($params)110 public function setParam($params) 108 111 { 109 112 $app =& App::getInstance(); … … 124 127 * @return mixed Configured parameter value. 125 128 */ 126 function getParam($param)129 public function getParam($param) 127 130 { 128 131 $app =& App::getInstance(); 129 132 130 133 if (isset($this->_params[$param])) { 131 134 return $this->_params[$param]; … … 138 141 /** 139 142 * Stores a new variable in the session cache. The $key should not be numeric 140 * because the array_shift function will reset the key to the next largest 143 * because the array_shift function will reset the key to the next largest 141 144 * int key. Weird behavior I can't understand. For example $cache["123"] will become $cache[0] 142 145 * 143 * @param str $key An identifier for the cached object. 144 * @param mixed $var The var to store in the session cache. 145 * @param bool $flags If we have something really big that we 146 * still want to cache, setting this to 147 * CACHE_ALLOW_OVERSIZED allows this. 148 * @return bool True on success, false otherwise. 149 */ 150 function set($key, $var, $flags=0) 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. 150 */ 151 public function set($key, $var, $allow_oversized=false) 151 152 { 152 153 $app =& App::getInstance(); … … 157 158 } 158 159 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 159 164 $var = serialize($var); 160 165 $var_len = mb_strlen($var); … … 165 170 } 166 171 167 if ($ flags & CACHE_ALLOW_OVERSIZED == 0&& $var_len >= $this->getParam('stack_size_limit')) {172 if ($allow_oversized && $var_len >= $this->getParam('stack_size_limit')) { 168 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__); 169 174 return false; 170 } 175 } 171 176 172 177 // Remove any value already stored under this key. … … 197 202 * @return mixed The requested datum, or false on failure. 198 203 */ 199 function get($key)204 public function get($key) 200 205 { 201 206 $app =& App::getInstance(); … … 226 231 * @return bool True if a value exists for the given key. 227 232 */ 228 function exists($key)233 public function exists($key) 229 234 { 230 235 $app =& App::getInstance(); … … 244 249 * @return bool True if the value existed before being unset. 245 250 */ 246 function delete($key)251 public function delete($key) 247 252 { 248 253 if (array_key_exists($key, $_SESSION['_cache'][$this->_ns])) { … … 253 258 } 254 259 } 255 260 256 261 /* 257 262 * Delete all existing items from the cache. … … 262 267 * @since 05 Jun 2006 23:51:34 263 268 */ 264 function clear()269 public function clear() 265 270 { 266 271 $_SESSION['_cache'][$this->_ns] = array(); … … 270 275 } 271 276 272 ?>
Note: See TracChangeset
for help on using the changeset viewer.