Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / objectcache / ObjectCacheSessionHandler.php
blob24d2a22e4305698d0225850c54f8d4f8f8a305bd
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 /** @var array Map of (session ID => SHA-1 of the data) */
32 protected static $hashCache = array();
34 /**
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' ) );
53 /**
54 * Get the cache storage object to use for session storage
55 * @return BagOStuff
57 protected static function getCache() {
58 global $wgSessionCacheType;
60 return ObjectCache::getInstance( $wgSessionCacheType );
63 /**
64 * Get a cache key for the given session id.
66 * @param string $id Session id
67 * @return string Cache key
69 protected static function getKey( $id ) {
70 return wfMemcKey( 'session', $id );
73 /**
74 * @param mixed $data
75 * @return string
77 protected static function getHash( $data ) {
78 return sha1( serialize( $data ) );
81 /**
82 * Callback when opening a session.
84 * @param string $save_path Path used to store session files, unused
85 * @param string $session_name Session name
86 * @return bool Success
88 static function open( $save_path, $session_name ) {
89 return true;
92 /**
93 * Callback when closing a session.
94 * NOP.
96 * @return bool Success
98 static function close() {
99 return true;
103 * Callback when reading session data.
105 * @param string $id Session id
106 * @return mixed Session data
108 static function read( $id ) {
109 $data = self::getCache()->get( self::getKey( $id ) );
111 self::$hashCache = array( $id => self::getHash( $data ) );
113 return ( $data === false ) ? '' : $data;
117 * Callback when writing session data.
119 * @param string $id Session id
120 * @param mixed $data Session data
121 * @return bool Success
123 static function write( $id, $data ) {
124 global $wgObjectCacheSessionExpiry;
126 // Only issue a write if anything changed (PHP 5.6 already does this)
127 if ( !isset( self::$hashCache[$id] )
128 || self::getHash( $data ) !== self::$hashCache[$id]
130 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
133 return true;
137 * Callback to destroy a session when calling session_destroy().
139 * @param string $id Session id
140 * @return bool Success
142 static function destroy( $id ) {
143 self::getCache()->delete( self::getKey( $id ) );
145 return true;
149 * Callback to execute garbage collection.
150 * NOP: Object caches perform garbage collection implicitly
152 * @param int $maxlifetime Maximum session life time
153 * @return bool Success
155 static function gc( $maxlifetime ) {
156 return true;
160 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
161 * for rationale.
163 static function handleShutdown() {
164 session_write_close();