source: tags/2.0.2/lib/SessionCache.inc.php @ 480

Last change on this file since 480 was 157, checked in by scdev, 18 years ago

Q - added tags/2.0.2 as the first php5 compatible version of the 2.0 branch.

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