File: memcache.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (233 lines) | stat: -rw-r--r-- 7,310 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
 * SessionHandler:: implementation for memcache.
 * memcache website: http://www.danga.com/memcached/
 *
 * Required parameters:<pre>
 *   'hostspec'  The hostname of the memcache server.
 *   'port'      The port on which to connect to the memcache server.</pre>
 *
 * Optional parameters:<pre>
 *   'persistent'  Use persistent DB connections? (boolean)
 *   'compression' Use compression when storing sessions.</pre>
 *
 * $Horde: framework/SessionHandler/SessionHandler/memcache.php,v 1.1.2.1 2006/05/02 17:27:49 chuck Exp $
 *
 * Copyright 2005-2006 Rong-En Fan <rafan@infor.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Rong-En Fan <rafan@infor.org>
 * @since   Horde 3.1
 * @package Horde_SessionHandler
 */
class SessionHandler_memcache extends SessionHandler {

    /**
     * Current memcache connection.
     *
     * @var object
     */
    var $_db;

    /**
     * Boolean indicating whether or not we're connected to the
     * memcache server.
     *
     * @var boolean
     */
    var $_connected = false;

    /**
     * Lock handle.
     *
     * @var resource
     */
    var $_fp;

    /**
     * Close the SessionHandler backend.
     *
     * @return boolean  True on success, false otherwise.
     */
    function close()
    {
        if ($this->_connected) {
            $this->_connected = false;
            @memcache_close($this->_db);
        }

        $this->_unlockSession();
        return true;
    }

    /**
     * Read the data for a particular session identifier from the
     * SessionHandler backend.
     *
     * @param string $id  The session identifier.
     *
     * @return string  The session data.
     */
    function read($id)
    {
        /* Obtain session lock. */
        $this->_lockSession($id);

        /* Make sure we have a valid memcache connection. */
        $this->_connect();

        $result = @memcache_get($this->_db, $id);
        if (!$result) {
            Horde::logMessage('Error retrieving session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
            $this->_unlockSession();
            @unlink($this->_params['lock_dir'] . '/lock_' . $id);
            return false;
        }

        Horde::logMessage('Read session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_DEBUG);
        return $result;
    }

    /**
     * Write session data to the SessionHandler backend.
     *
     * @param string $id            The session identifier.
     * @param string $session_data  The session data.
     *
     * @return boolean  True on success, false otherwise.
     */
    function write($id, $session_data)
    {
        /* Make sure we have a valid memcache connection. */
        $this->_connect();

        $lifetime = ini_get('session.gc_maxlifetime');
        $flags = $this->_params['compression'] ? MEMCACHE_COMPRESSED : 0;
        $result = @memcache_set($this->_db, $id, $session_data, $flags, $lifetime);

        if (!$result) {
            Horde::logMessage('Error writing session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
            return false;
        }
        Horde::logMessage('Wrote session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_DEBUG);

        return true;
    }

    /**
     * Destroy the data for a particular session identifier in the
     * SessionHandler backend.
     *
     * @param string $id  The session identifier.
     *
     * @return boolean  True on success, false otherwise.
     */
    function destroy($id)
    {
        /* Make sure we have a valid memcache connection. */
        $this->_connect();

        $result = @memcache_delete($this->_db, $id);

        if (!$result) {
            Horde::logMessage('Failed to delete session (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_ERR);
            return false;
        }
        Horde::logMessage('Deleted session data (id = ' . $id . ')', __FILE__, __LINE__, PEAR_LOG_DEBUG);

        $this->_unlockSession();
        @unlink($this->_params['lock_dir'] . '/lock_' . $id);
        return true;
    }

    /**
     * Garbage collect stale sessions from the SessionHandler backend.
     *
     * @param integer $maxlifetime  The maximum age of a session.
     *
     * @return boolean  True on success, false otherwise.
     */
    function gc($maxlifetime = 300)
    {
        return true;
    }

    /**
     * Attempts to open a connection to the memcache server(s).
     *
     * @access private
     */
    function _connect()
    {
        if ($this->_connected) {
            Horde::logMessage('Already connected to a memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_DEBUG);
            return;
        }

        Horde::assertDriverConfig($this->_params, 'sessionhandler',
                                  array('hostspec', 'port'),
                                  'session handler memcache');

        if (empty($this->_params['persistent'])) {
            $connect_persistent = false;
        } else {
            $connect_persistent = true;
        }
        $con = new Memcache;

        for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) {
            if (!@$con->addServer($this->_params['hostspec'][$i],
                                  $this->_params['port'][$i],
                                  $connect_persistent)) {
                Horde::logMessage('Could not add [' . $this->_params['hostspec'][$i] . ':' . $this->_params['port'][$i] . '] as memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_ERR);
            } else {
                Horde::logMessage('Added [' . $this->_params['hostspec'][$i] . ':' . $this->_params['port'][$i] . '] as memcache server for memcache SessionHandler', __FILE__, __LINE__, PEAR_LOG_DEBUG);
                $this->_connected = true;
            }
        }

        /* Check if any of the pooled connections works. */
        if ($con->getVersion()) {
            $this->_db = $con;
            $this->_connected = true;
            Horde::logMessage('Connected to a memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_DEBUG);
        } else {
            $this->_connected = false;
            Horde::logMessage('Could not connect to any memcache server for memcache SessionHandler' , __FILE__, __LINE__,PEAR_LOG_ERR);
        }
    }

    /**
     * @access private
     */
    function _lockSession($id)
    {
        $this->_fp = @fopen($this->_params['lock_dir'] . '/lock_' . $id, 'w+');
        if (!$this->_fp) {
            Horde::logMessage('Could not open session lock for ' . $id, __FILE__, __LINE__, PEAR_LOG_ERR);
            return false;
        }
        if (!@flock($this->_fp, LOCK_EX)) {
            Horde::logMessage('Could not lock session ' . $id, __FILE__, __LINE__, PEAR_LOG_ERR);
            return false;
        }

        return true;
    }

    /**
     * @access private
     */
    function _unlockSession()
    {
        if (is_resource($this->_fp)) {
            if (!@flock($this->_fp, LOCK_UN)) {
                Horde::logMessage('Could not unlock session', __FILE__, __LINE__, PEAR_LOG_ERR);
            }
            @fclose($this->_fp);
        }
    }

}