[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * SessionCache.inc.php |
---|
| 4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * The maximum byte size that the session cache will hold. |
---|
| 9 | * @const SESSION_CACHE_SIZE_BYTES |
---|
| 10 | */ |
---|
| 11 | if (!defined('SESSION_CACHE_SIZE_BYTES')) { |
---|
| 12 | define('SESSION_CACHE_SIZE_BYTES', 32768); // 32 Kilobytes. |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * The cache will never flush out all the items, it will leave this many in. |
---|
| 17 | * This is in case you cache a very large item and expect it to stay a little |
---|
| 18 | * longer than having if dumped the next time something goes into the cache. |
---|
| 19 | * @const SESSION_CACHE_MIN_ITEMS |
---|
| 20 | */ |
---|
| 21 | define('SESSION_CACHE_MIN_ITEMS', 3); // 3 items. |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * SessionCache:: provides an API for storing a limited amount of data |
---|
| 26 | * intended to have a short lifetime in a user's session. |
---|
| 27 | * |
---|
| 28 | * @inspiration Horde 2.0's SessionCache class. |
---|
| 29 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 30 | * @version 1.0 |
---|
| 31 | */ |
---|
| 32 | class SessionCache |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Stores a new variable in the session cache. The $var_id is is md5'ed |
---|
| 37 | * because if a variable id is a very large integer, the array_shift function |
---|
| 38 | * will reset the key to the next largest int key. Weird behaviour I can't |
---|
| 39 | * understand. $session_cache[32341234123] will become $session_cache[0] |
---|
| 40 | * for example. Usage warning: if the variable is too big to fit, or is |
---|
| 41 | * old and discarded, you must provide alternative ways of accessing the data. |
---|
| 42 | * |
---|
| 43 | * @param mixed $var The var to store in the session cache. |
---|
| 44 | * @param str $var_id An identifyer for the cached object. |
---|
| 45 | * @param bool $force_it_in If we have something really big that we |
---|
| 46 | * still want to cache, setting this true |
---|
| 47 | * allows this. |
---|
| 48 | * |
---|
| 49 | * @return string The $var_id, or false if the object was too big to cache. |
---|
| 50 | */ |
---|
| 51 | function putCache($var, $var_id, $force_it_in=false) |
---|
| 52 | { |
---|
| 53 | $var_id = md5($var_id); |
---|
| 54 | $serialized_var = serialize($var); |
---|
| 55 | $serialized_var_len = strlen($serialized_var); |
---|
| 56 | |
---|
| 57 | if ($serialized_var_len >= 1024000) { |
---|
| 58 | logMsg(sprintf('Attempting to cache oversized variable at %s bytes.', $serialized_var_len), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if ($serialized_var_len >= SESSION_CACHE_SIZE_BYTES && !$force_it_in) { |
---|
| 62 | return false; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if (!isset($_SESSION['_session_cache'])) { |
---|
| 66 | $_SESSION['_session_cache'] = array(); |
---|
| 67 | } else { |
---|
| 68 | unset($_SESSION['_session_cache'][$var_id]); |
---|
| 69 | // 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. |
---|
| 70 | while (strlen(serialize($_SESSION['_session_cache'])) + $serialized_var_len >= SESSION_CACHE_SIZE_BYTES |
---|
| 71 | && sizeof($_SESSION['_session_cache']) >= SESSION_CACHE_MIN_ITEMS) { |
---|
| 72 | array_shift($_SESSION['_session_cache']); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | $_SESSION['_session_cache'][$var_id] =& $serialized_var; |
---|
| 76 | |
---|
| 77 | return $var_id; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Retrives an object from the session cache and returns it unserialized. |
---|
| 82 | * It also moves it to the top of the stack, which makes it such that the |
---|
| 83 | * cache flushing mechanism of putCache deletes the oldest referenced items |
---|
| 84 | * first. |
---|
| 85 | * |
---|
| 86 | * @param string $var_id The identifyer for the datum to retrieve. |
---|
| 87 | * |
---|
| 88 | * @return mixed The requested datum, or false on failure. |
---|
| 89 | */ |
---|
| 90 | function getCache($var_id) |
---|
| 91 | { |
---|
| 92 | $var_id = md5($var_id); |
---|
| 93 | if (isset($_SESSION['_session_cache'][$var_id])) { |
---|
| 94 | // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this? |
---|
| 95 | $tmp =& $_SESSION['_session_cache'][$var_id]; |
---|
| 96 | unset($_SESSION['_session_cache'][$var_id]); |
---|
| 97 | $_SESSION['_session_cache'][$var_id] =& $tmp; |
---|
| 98 | // Return the unserialized datum. |
---|
| 99 | return unserialize($_SESSION['_session_cache'][$var_id]); |
---|
| 100 | } else { |
---|
| 101 | return false; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Tells you if the object is cached. |
---|
| 107 | * |
---|
| 108 | * @param string $var_id The ID of the object to check. |
---|
| 109 | * |
---|
| 110 | * @return bool The return from isset(). |
---|
| 111 | */ |
---|
| 112 | function isCached($var_id) |
---|
| 113 | { |
---|
| 114 | $var_id = md5($var_id); |
---|
| 115 | return isset($_SESSION['_session_cache'][$var_id]); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Tells you if the object is cached. |
---|
| 120 | * |
---|
| 121 | * @param string $var_id The ID of the object to check. |
---|
| 122 | * |
---|
| 123 | * @return bool The return from isset(). |
---|
| 124 | */ |
---|
| 125 | function breakCache($var_id) |
---|
| 126 | { |
---|
| 127 | $var_id = md5($var_id); |
---|
| 128 | unset($_SESSION['_session_cache'][$var_id]); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | // END SessionCache |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | ?> |
---|