3 * Session storage in object cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Session storage in object cache.
26 * Used if $wgSessionsInObjectCache is true.
30 class ObjectCacheSessionHandler
{
32 const TTL_REFRESH_WINDOW
= 600; // refresh if expiring in 10 minutes
35 * Install a session handler for the current web request
37 static function install() {
38 session_set_save_handler(
39 array( __CLASS__
, 'open' ),
40 array( __CLASS__
, 'close' ),
41 array( __CLASS__
, 'read' ),
42 array( __CLASS__
, 'write' ),
43 array( __CLASS__
, 'destroy' ),
44 array( __CLASS__
, 'gc' ) );
46 // It's necessary to register a shutdown function to call session_write_close(),
47 // because by the time the request shutdown function for the session module is
48 // called, $wgMemc has already been destroyed. Shutdown functions registered
49 // this way are called before object destruction.
50 register_shutdown_function( array( __CLASS__
, 'handleShutdown' ) );
54 * Get the cache storage object to use for session storage
57 protected static function getCache() {
58 global $wgSessionCacheType;
59 return ObjectCache
::getInstance( $wgSessionCacheType );
63 * Get a cache key for the given session id.
65 * @param string $id Session id
66 * @return string Cache key
68 protected static function getKey( $id ) {
69 return wfMemcKey( 'session', $id );
73 * Callback when opening a session.
75 * @param string $save_path Path used to store session files, unused
76 * @param string $session_name Session name
77 * @return bool Success
79 static function open( $save_path, $session_name ) {
84 * Callback when closing a session.
87 * @return bool Success
89 static function close() {
94 * Callback when reading session data.
96 * @param string $id Session id
97 * @return mixed Session data
99 static function read( $id ) {
100 $data = self
::getCache()->get( self
::getKey( $id ) );
101 if ( $data === false ) {
108 * Callback when writing session data.
110 * @param string $id Session id
111 * @param string $data Session data
112 * @return bool Success
114 static function write( $id, $data ) {
115 global $wgObjectCacheSessionExpiry;
116 self
::getCache()->set( self
::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
121 * Callback to destroy a session when calling session_destroy().
123 * @param string $id Session id
124 * @return bool Success
126 static function destroy( $id ) {
127 self
::getCache()->delete( self
::getKey( $id ) );
133 * Callback to execute garbage collection.
134 * NOP: Object caches perform garbage collection implicitly
136 * @param int $maxlifetime Maximum session life time
137 * @return bool Success
139 static function gc( $maxlifetime ) {
145 * See the comment inside ObjectCacheSessionHandler::install for rationale.
147 static function handleShutdown() {
148 global $wgObjectCacheSessionExpiry;
150 $now = microtime( true );
151 // Session are only written in object stores when $_SESSION changes,
152 // which also renews the TTL ($wgObjectCacheSessionExpiry). If a user
153 // is active but not causing session data changes, it may suddenly
154 // as they view a form, blocking the first submission.
155 // Make a dummy change every so often to avoid this.
156 if ( !isset( $_SESSION['wsExpiresUnix'] )
157 ||
( $now + self
::TTL_REFRESH_WINDOW
) > isset( $_SESSION['wsExpiresUnix'] )
159 $_SESSION['wsExpiresUnix'] = $now +
$wgObjectCacheSessionExpiry;
162 session_write_close();