Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / objectcache / ObjectCacheSessionHandler.php
blobcdf8da1eaa96042ebe0df832c2bfda44db5742cd
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
52 * @return ObjectCache
54 static function getCache() {
55 global $wgSessionCacheType;
56 return ObjectCache::getInstance( $wgSessionCacheType );
59 /**
60 * Get a cache key for the given session id.
62 * @param string $id Session id
63 * @return string Cache key
65 static function getKey( $id ) {
66 return wfMemcKey( 'session', $id );
69 /**
70 * Callback when opening a session.
72 * @param string $save_path Path used to store session files, unused
73 * @param string $session_name Session name
74 * @return bool Success
76 static function open( $save_path, $session_name ) {
77 return true;
80 /**
81 * Callback when closing a session.
82 * NOP.
84 * @return bool Success
86 static function close() {
87 return true;
90 /**
91 * Callback when reading session data.
93 * @param string $id Session id
94 * @return mixed Session data
96 static function read( $id ) {
97 $data = self::getCache()->get( self::getKey( $id ) );
98 if ( $data === false ) {
99 return '';
101 return $data;
105 * Callback when writing session data.
107 * @param string $id Session id
108 * @param mixed $data Session data
109 * @return bool Success
111 static function write( $id, $data ) {
112 global $wgObjectCacheSessionExpiry;
113 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
114 return true;
118 * Callback to destroy a session when calling session_destroy().
120 * @param string $id Session id
121 * @return bool Success
123 static function destroy( $id ) {
124 self::getCache()->delete( self::getKey( $id ) );
125 return true;
129 * Callback to execute garbage collection.
130 * NOP: Object caches perform garbage collection implicitly
132 * @param int $maxlifetime Maximum session life time
133 * @return bool Success
135 static function gc( $maxlifetime ) {
136 return true;
140 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
141 * for rationale.
143 static function handleShutdown() {
144 session_write_close();