Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / objectcache / ObjectCacheSessionHandler.php
blob7cf960e7d0660ba61dc21d1214f923ac44da96f7
1 <?php
2 /**
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
20 * @file
21 * @ingroup Cache
24 /**
25 * Session storage in object cache.
26 * Used if $wgSessionsInObjectCache is true.
28 * @ingroup Cache
30 class ObjectCacheSessionHandler {
31 /**
32 * Install a session handler for the current web request
34 static function install() {
35 session_set_save_handler(
36 array( __CLASS__, 'open' ),
37 array( __CLASS__, 'close' ),
38 array( __CLASS__, 'read' ),
39 array( __CLASS__, 'write' ),
40 array( __CLASS__, 'destroy' ),
41 array( __CLASS__, 'gc' ) );
43 // It's necessary to register a shutdown function to call session_write_close(),
44 // because by the time the request shutdown function for the session module is
45 // called, $wgMemc has already been destroyed. Shutdown functions registered
46 // this way are called before object destruction.
47 register_shutdown_function( array( __CLASS__, 'handleShutdown' ) );
50 /**
51 * Get the cache storage object to use for session storage
53 static function getCache() {
54 global $wgSessionCacheType;
55 return ObjectCache::getInstance( $wgSessionCacheType );
58 /**
59 * Get a cache key for the given session id.
61 * @param string $id session id
62 * @return String: cache key
64 static function getKey( $id ) {
65 return wfMemcKey( 'session', $id );
68 /**
69 * Callback when opening a session.
71 * @param $save_path String: path used to store session files, unused
72 * @param $session_name String: session name
73 * @return Boolean: success
75 static function open( $save_path, $session_name ) {
76 return true;
79 /**
80 * Callback when closing a session.
81 * NOP.
83 * @return Boolean: success
85 static function close() {
86 return true;
89 /**
90 * Callback when reading session data.
92 * @param string $id session id
93 * @return Mixed: session data
95 static function read( $id ) {
96 $data = self::getCache()->get( self::getKey( $id ) );
97 if ( $data === false ) {
98 return '';
100 return $data;
104 * Callback when writing session data.
106 * @param string $id session id
107 * @param $data Mixed: session data
108 * @return Boolean: success
110 static function write( $id, $data ) {
111 global $wgObjectCacheSessionExpiry;
112 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
113 return true;
117 * Callback to destroy a session when calling session_destroy().
119 * @param string $id session id
120 * @return Boolean: success
122 static function destroy( $id ) {
123 self::getCache()->delete( self::getKey( $id ) );
124 return true;
128 * Callback to execute garbage collection.
129 * NOP: Object caches perform garbage collection implicitly
131 * @param $maxlifetime Integer: maximum session life time
132 * @return Boolean: success
134 static function gc( $maxlifetime ) {
135 return true;
139 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
140 * for rationale.
142 static function handleShutdown() {
143 session_write_close();