Changeset 136 for trunk/lib/SessionCache.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/SessionCache.inc.php
r119 r136 1 1 <?php 2 2 /** 3 * SessionCache.inc.php 3 * Cache.inc.php 4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information 5 * 4 6 * Provides an API for storing a limited amount of data 5 7 * intended to have a short lifetime in a user's session. 6 8 * 7 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.8 9 * @author Quinn Comendant <quinn@strangecode.com> 9 * @version 1.210 * @version 2.1 10 11 * @since 2001 11 12 */ 12 class SessionCache 13 { 13 14 // Flags. 15 define('CACHE_IGNORE_SIZE', 1); 16 17 class Cache { 18 14 19 var $_params = array( 15 20 'enabled' => true, … … 22 27 * This method enforces the singleton pattern for this class. 23 28 * 24 * @return object Reference to the global SessionCache object.29 * @return object Reference to the global Cache object. 25 30 * @access public 26 31 * @static 27 32 */ 28 function &getInstance() { 33 function &getInstance() 34 { 29 35 static $instance = null; 30 36 31 37 if ($instance === null) { 32 $instance = new SessionCache();38 $instance = new Cache(); 33 39 } 34 40 … … 44 50 function setParam($params) 45 51 { 46 if (!isset($_this) || !is_a($_this, 'SessionCache') && !is_subclass_of($_this, 'SessionCache')) { 47 $_this =& SessionCache::getInstance(); 48 } 52 $app =& App::getInstance(); 49 53 50 54 if (isset($params) && is_array($params)) { 51 55 // 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__); 55 59 } 56 60 } … … 65 69 function getParam($param) 66 70 { 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__); 75 77 return null; 76 78 } … … 78 80 79 81 /** 80 * Stores a new variable in the session cache. The $ var_idis is md5'ed82 * Stores a new variable in the session cache. The $key is is md5'ed 81 83 * 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 behavio ur I can't84 * will reset the key to the next largest int key. Weird behavior I can't 83 85 * understand. $session_cache[32341234123] will become $session_cache[0] 84 86 * for example. Usage warning: if the variable is too big to fit, or is 85 87 * old and discarded, you must provide alternative ways of accessing the data. 86 88 * 89 * @param str $key An identifier for the cached object. 87 90 * @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); 107 107 $serialized_var = serialize($var); 108 108 $serialized_var_len = strlen($serialized_var); 109 109 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__); 117 117 return false; 118 118 } … … 121 121 $_SESSION['_session_cache'] = array(); 122 122 } else { 123 unset($_SESSION['_session_cache'][$ var_id]);123 unset($_SESSION['_session_cache'][$key]); 124 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')) {125 while (strlen(serialize($_SESSION['_session_cache'])) + $serialized_var_len >= $this->getParam('soft_limit') 126 && sizeof($_SESSION['_session_cache']) >= $this->getParam('min_items')) { 127 127 array_shift($_SESSION['_session_cache']); 128 128 } 129 129 } 130 $_SESSION['_session_cache'][$ var_id] =& $serialized_var;130 $_SESSION['_session_cache'][$key] =& $serialized_var; 131 131 132 132 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; 137 137 } 138 138 … … 143 143 * first. 144 144 * 145 * @param string $ var_id The identifyerfor the datum to retrieve.145 * @param string $key The key for the datum to retrieve. 146 146 * 147 147 * @return mixed The requested datum, or false on failure. 148 148 */ 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])) { 161 157 // 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; 165 161 // Return the unserialized datum. 166 return unserialize($_SESSION['_session_cache'][$ var_id]);162 return unserialize($_SESSION['_session_cache'][$key]); 167 163 } else { 168 164 return false; … … 173 169 * Tells you if the object is cached. 174 170 * 175 * @param string $ var_id The IDof the object to check.171 * @param string $key The key of the object to check. 176 172 * 177 173 * @return bool The return from isset(). 178 174 */ 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]); 191 183 } 192 184 … … 194 186 * Tells you if the object is cached. 195 187 * 196 * @param string $ var_id The IDof the object to check.188 * @param string $key The key of the object to check. 197 189 * 198 190 * @return bool The return from isset(). 199 191 */ 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 SessionCache192 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 209 201 } 210 202
Note: See TracChangeset
for help on using the changeset viewer.