source: trunk/lib/SessionCache.inc.php @ 42

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

detabbed all files ;P

File size: 7.0 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        if (!isset($this) || !is_a($this, 'SessionCache')) {
47            $this =& SessionCache::getInstance();
48        }
49
50        if (isset($params) && is_array($params)) {
51            // 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__);
55        }
56    }
57
58    /**
59     * Return the value of a parameter, if it exists.
60     *
61     * @access public
62     * @param string $param        Which parameter to return.
63     * @return mixed               Configured parameter value.
64     */
65    function getParam($param)
66    {
67        if (!isset($this) || !is_a($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__);
75            return null;
76        }
77    }
78
79    /**
80     * Stores a new variable in the session cache. The $var_id is is md5'ed
81     * 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 behaviour I can't
83     * understand. $session_cache[32341234123] will become $session_cache[0]
84     * for example. Usage warning: if the variable is too big to fit, or is
85     * old and discarded, you must provide alternative ways of accessing the data.
86     *
87     * @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')) {
98            $this =& SessionCache::getInstance();
99        }
100
101        if (!$this->getParam('enabled')) {
102            return false;
103        }
104
105        $var_id = md5($var_id);
106        $serialized_var = serialize($var);
107        $serialized_var_len = strlen($serialized_var);
108
109        if ($serialized_var_len >= $this->getParam('soft_limit') && !$force_it_in) {
110            App::logMsg(sprintf('Serialized variable (%s bytes) more than soft_limit (%s bytes).', $serialized_var_len, $this->getParam('soft_limit')), LOG_NOTICE, __FILE__, __LINE__);
111            return false;
112        }
113
114        if ($serialized_var_len >= $this->getParam('hard_limit')) {
115            App::logMsg(sprintf('Serialized variable (%s bytes) more than hard_limit (%s bytes).', $serialized_var_len, $this->getParam('hard_limit')), LOG_NOTICE, __FILE__, __LINE__);
116            return false;
117        }
118
119        if (!isset($_SESSION['_session_cache'])) {
120            $_SESSION['_session_cache'] = array();
121        } else {
122            unset($_SESSION['_session_cache'][$var_id]);
123            // 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.
124            while (strlen(serialize($_SESSION['_session_cache'])) + $serialized_var_len >= $this->getParam('soft_limit')
125            && sizeof($_SESSION['_session_cache']) >= $this->getParam('min_items')) {
126                array_shift($_SESSION['_session_cache']);
127            }
128        }
129        $_SESSION['_session_cache'][$var_id] =& $serialized_var;
130
131        if ($serialized_var_len >= 1024000) {
132            App::logMsg(sprintf('Successfully cached oversized variable (%s bytes).', $serialized_var_len), LOG_DEBUG, __FILE__, __LINE__);
133        }
134
135        return $var_id;
136    }
137
138    /**
139     * Retrives an object from the session cache and returns it unserialized.
140     * It also moves it to the top of the stack, which makes it such that the
141     * cache flushing mechanism of putCache deletes the oldest referenced items
142     * first.
143     *
144     * @param string $var_id  The identifyer for the datum to retrieve.
145     *
146     * @return mixed          The requested datum, or false on failure.
147     */
148    function getCache($var_id)
149    {
150        if (!isset($this) || !is_a($this, 'SessionCache')) {
151            $this =& SessionCache::getInstance();
152        }
153
154        if (!$this->getParam('enabled')) {
155            return false;
156        }
157
158        $var_id = md5($var_id);
159        if (isset($_SESSION['_session_cache'][$var_id])) {
160            // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this?
161            $tmp =& $_SESSION['_session_cache'][$var_id];
162            unset($_SESSION['_session_cache'][$var_id]);
163            $_SESSION['_session_cache'][$var_id] =& $tmp;
164            // Return the unserialized datum.
165            return unserialize($_SESSION['_session_cache'][$var_id]);
166        } else {
167            return false;
168        }
169    }
170
171    /**
172     * Tells you if the object is cached.
173     *
174     * @param string $var_id  The ID of the object to check.
175     *
176     * @return bool           The return from isset().
177     */
178    function isCached($var_id)
179    {
180        if (!isset($this) || !is_a($this, 'SessionCache')) {
181            $this =& SessionCache::getInstance();
182        }
183
184        if (!$this->getParam('enabled')) {
185            return false;
186        }
187
188        $var_id = md5($var_id);
189        return isset($_SESSION['_session_cache'][$var_id]);
190    }
191
192    /**
193     * Tells you if the object is cached.
194     *
195     * @param string $var_id  The ID of the object to check.
196     *
197     * @return bool           The return from isset().
198     */
199    function breakCache($var_id)
200    {
201        $var_id = md5($var_id);
202        if (isset($_SESSION['_session_cache'][$var_id])) {
203            unset($_SESSION['_session_cache'][$var_id]);
204        }
205    }
206
207// END SessionCache
208}
209
210?>
Note: See TracBrowser for help on using the repository browser.