[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * SessionCache.inc.php |
---|
| 4 | * Provides an API for storing a limited amount of data |
---|
| 5 | * intended to have a short lifetime in a user's session. |
---|
| 6 | * |
---|
| 7 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information. |
---|
| 8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 9 | * @version 1.2 |
---|
| 10 | * @since 2001 |
---|
| 11 | */ |
---|
| 12 | class SessionCache |
---|
| 13 | { |
---|
| 14 | var $_params = array( |
---|
| 15 | 'soft_limit' => 204800, |
---|
| 16 | 'hard_limit' => 4194304, |
---|
| 17 | 'min_items' => 3, |
---|
| 18 | ); |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * This method enforces the singleton pattern for this class. |
---|
| 22 | * |
---|
| 23 | * @return object Reference to the global SessionCache object. |
---|
| 24 | * @access public |
---|
| 25 | * @static |
---|
| 26 | */ |
---|
| 27 | function &getInstance() { |
---|
| 28 | static $instance = null; |
---|
| 29 | |
---|
| 30 | if ($instance === null) { |
---|
| 31 | $instance = new SessionCache(); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | return $instance; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 39 | * |
---|
| 40 | * @access public |
---|
| 41 | * @param array $params Array of parameters (key => val pairs). |
---|
| 42 | */ |
---|
| 43 | function setParam($params) |
---|
| 44 | { |
---|
| 45 | if (isset($params) && is_array($params)) { |
---|
| 46 | // Merge new parameters with old overriding only those passed. |
---|
| 47 | $this->_params = array_merge($this->_params, $params); |
---|
| 48 | } else { |
---|
| 49 | App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Return the value of a parameter, if it exists. |
---|
| 55 | * |
---|
| 56 | * @access public |
---|
| 57 | * @param string $param Which parameter to return. |
---|
| 58 | * @return mixed Configured parameter value. |
---|
| 59 | */ |
---|
| 60 | function getParam($param) |
---|
| 61 | { |
---|
| 62 | if (isset($this->_params[$param])) { |
---|
| 63 | return $this->_params[$param]; |
---|
| 64 | } else { |
---|
| 65 | App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 66 | return null; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Stores a new variable in the session cache. The $var_id is is md5'ed |
---|
| 72 | * because if a variable id is a very large integer, the array_shift function |
---|
| 73 | * will reset the key to the next largest int key. Weird behaviour I can't |
---|
| 74 | * understand. $session_cache[32341234123] will become $session_cache[0] |
---|
| 75 | * for example. Usage warning: if the variable is too big to fit, or is |
---|
| 76 | * old and discarded, you must provide alternative ways of accessing the data. |
---|
| 77 | * |
---|
| 78 | * @param mixed $var The var to store in the session cache. |
---|
| 79 | * @param str $var_id An identifyer for the cached object. |
---|
| 80 | * @param bool $force_it_in If we have something really big that we |
---|
| 81 | * still want to cache, setting this true |
---|
| 82 | * allows this. |
---|
| 83 | * |
---|
| 84 | * @return string The $var_id, or false if the object was too big to cache. |
---|
| 85 | */ |
---|
| 86 | function putCache($var, $var_id, $force_it_in=false) |
---|
| 87 | { |
---|
| 88 | $cache =& SessionCache::getInstance(); |
---|
| 89 | |
---|
| 90 | $var_id = md5($var_id); |
---|
| 91 | $serialized_var = serialize($var); |
---|
| 92 | $serialized_var_len = strlen($serialized_var); |
---|
| 93 | |
---|
| 94 | if ($serialized_var_len >= $cache->getParam('soft_limit') && !$force_it_in) { |
---|
| 95 | App::logMsg(sprintf('Serialized variable (%s bytes) more than soft_limit (%s bytes).', $serialized_var_len, $cache->getParam('soft_limit')), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 96 | return false; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if ($serialized_var_len >= $cache->getParam('hard_limit')) { |
---|
| 100 | App::logMsg(sprintf('Serialized variable (%s bytes) more than hard_limit (%s bytes).', $serialized_var_len, $cache->getParam('hard_limit')), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 101 | return false; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if (!isset($_SESSION['_session_cache'])) { |
---|
| 105 | $_SESSION['_session_cache'] = array(); |
---|
| 106 | } else { |
---|
| 107 | unset($_SESSION['_session_cache'][$var_id]); |
---|
| 108 | // 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. |
---|
| 109 | while (strlen(serialize($_SESSION['_session_cache'])) + $serialized_var_len >= $cache->getParam('soft_limit') |
---|
| 110 | && sizeof($_SESSION['_session_cache']) >= $cache->getParam('min_items')) { |
---|
| 111 | array_shift($_SESSION['_session_cache']); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | $_SESSION['_session_cache'][$var_id] =& $serialized_var; |
---|
| 115 | |
---|
| 116 | if ($serialized_var_len >= 1024000) { |
---|
| 117 | App::logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $serialized_var_len), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | return $var_id; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Retrives an object from the session cache and returns it unserialized. |
---|
| 125 | * It also moves it to the top of the stack, which makes it such that the |
---|
| 126 | * cache flushing mechanism of putCache deletes the oldest referenced items |
---|
| 127 | * first. |
---|
| 128 | * |
---|
| 129 | * @param string $var_id The identifyer for the datum to retrieve. |
---|
| 130 | * |
---|
| 131 | * @return mixed The requested datum, or false on failure. |
---|
| 132 | */ |
---|
| 133 | function getCache($var_id) |
---|
| 134 | { |
---|
| 135 | $var_id = md5($var_id); |
---|
| 136 | if (isset($_SESSION['_session_cache'][$var_id])) { |
---|
| 137 | // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this? |
---|
| 138 | $tmp =& $_SESSION['_session_cache'][$var_id]; |
---|
| 139 | unset($_SESSION['_session_cache'][$var_id]); |
---|
| 140 | $_SESSION['_session_cache'][$var_id] =& $tmp; |
---|
| 141 | // Return the unserialized datum. |
---|
| 142 | return unserialize($_SESSION['_session_cache'][$var_id]); |
---|
| 143 | } else { |
---|
| 144 | return false; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Tells you if the object is cached. |
---|
| 150 | * |
---|
| 151 | * @param string $var_id The ID of the object to check. |
---|
| 152 | * |
---|
| 153 | * @return bool The return from isset(). |
---|
| 154 | */ |
---|
| 155 | function isCached($var_id) |
---|
| 156 | { |
---|
| 157 | $var_id = md5($var_id); |
---|
| 158 | return isset($_SESSION['_session_cache'][$var_id]); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Tells you if the object is cached. |
---|
| 163 | * |
---|
| 164 | * @param string $var_id The ID of the object to check. |
---|
| 165 | * |
---|
| 166 | * @return bool The return from isset(). |
---|
| 167 | */ |
---|
| 168 | function breakCache($var_id) |
---|
| 169 | { |
---|
| 170 | $var_id = md5($var_id); |
---|
| 171 | if (isset($_SESSION['_session_cache'][$var_id])) { |
---|
| 172 | unset($_SESSION['_session_cache'][$var_id]); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | // END SessionCache |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | ?> |
---|