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
23 class ObjectCacheSessionHandler
{
25 * Install a session handler for the current web request
27 static function install() {
28 session_set_save_handler(
29 array( __CLASS__
, 'open' ),
30 array( __CLASS__
, 'close' ),
31 array( __CLASS__
, 'read' ),
32 array( __CLASS__
, 'write' ),
33 array( __CLASS__
, 'destroy' ),
34 array( __CLASS__
, 'gc' ) );
36 // It's necessary to register a shutdown function to call session_write_close(),
37 // because by the time the request shutdown function for the session module is
38 // called, $wgMemc has already been destroyed. Shutdown functions registered
39 // this way are called before object destruction.
40 register_shutdown_function( array( __CLASS__
, 'handleShutdown' ) );
44 * Get the cache storage object to use for session storage
46 static function getCache() {
47 global $wgSessionCacheType;
48 return ObjectCache
::getInstance( $wgSessionCacheType );
52 * Get a cache key for the given session id.
54 * @param $id String: session id
55 * @return String: cache key
57 static function getKey( $id ) {
58 return wfMemcKey( 'session', $id );
62 * Callback when opening a session.
64 * @param $save_path String: path used to store session files, unused
65 * @param $session_name String: session name
66 * @return Boolean: success
68 static function open( $save_path, $session_name ) {
73 * Callback when closing a session.
76 * @return Boolean: success
78 static function close() {
83 * Callback when reading session data.
85 * @param $id String: session id
86 * @return Mixed: session data
88 static function read( $id ) {
89 $data = self
::getCache()->get( self
::getKey( $id ) );
90 if( $data === false ) {
97 * Callback when writing session data.
99 * @param $id String: session id
100 * @param $data Mixed: session data
101 * @return Boolean: success
103 static function write( $id, $data ) {
104 global $wgObjectCacheSessionExpiry;
105 self
::getCache()->set( self
::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
110 * Callback to destroy a session when calling session_destroy().
112 * @param $id String: session id
113 * @return Boolean: success
115 static function destroy( $id ) {
116 self
::getCache()->delete( self
::getKey( $id ) );
121 * Callback to execute garbage collection.
122 * NOP: Object caches perform garbage collection implicitly
124 * @param $maxlifetime Integer: maximum session life time
125 * @return Boolean: success
127 static function gc( $maxlifetime ) {
132 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
135 static function handleShutdown() {
136 session_write_close();