Merge "Allow session expiry time to be configured"
[lhc/web/wiklou.git] / includes / objectcache / ObjectCacheSessionHandler.php
1 <?php
2 /*
3 * Session storage in object cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23 class ObjectCacheSessionHandler {
24 /**
25 * Install a session handler for the current web request
26 */
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' ) );
35
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' ) );
41 }
42
43 /**
44 * Get the cache storage object to use for session storage
45 */
46 static function getCache() {
47 global $wgSessionCacheType;
48 return ObjectCache::getInstance( $wgSessionCacheType );
49 }
50
51 /**
52 * Get a cache key for the given session id.
53 *
54 * @param $id String: session id
55 * @return String: cache key
56 */
57 static function getKey( $id ) {
58 return wfMemcKey( 'session', $id );
59 }
60
61 /**
62 * Callback when opening a session.
63 *
64 * @param $save_path String: path used to store session files, unused
65 * @param $session_name String: session name
66 * @return Boolean: success
67 */
68 static function open( $save_path, $session_name ) {
69 return true;
70 }
71
72 /**
73 * Callback when closing a session.
74 * NOP.
75 *
76 * @return Boolean: success
77 */
78 static function close() {
79 return true;
80 }
81
82 /**
83 * Callback when reading session data.
84 *
85 * @param $id String: session id
86 * @return Mixed: session data
87 */
88 static function read( $id ) {
89 $data = self::getCache()->get( self::getKey( $id ) );
90 if( $data === false ) {
91 return '';
92 }
93 return $data;
94 }
95
96 /**
97 * Callback when writing session data.
98 *
99 * @param $id String: session id
100 * @param $data Mixed: session data
101 * @return Boolean: success
102 */
103 static function write( $id, $data ) {
104 global $wgObjectCacheSessionExpiry;
105 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
106 return true;
107 }
108
109 /**
110 * Callback to destroy a session when calling session_destroy().
111 *
112 * @param $id String: session id
113 * @return Boolean: success
114 */
115 static function destroy( $id ) {
116 self::getCache()->delete( self::getKey( $id ) );
117 return true;
118 }
119
120 /**
121 * Callback to execute garbage collection.
122 * NOP: Object caches perform garbage collection implicitly
123 *
124 * @param $maxlifetime Integer: maximum session life time
125 * @return Boolean: success
126 */
127 static function gc( $maxlifetime ) {
128 return true;
129 }
130
131 /**
132 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
133 * for rationale.
134 */
135 static function handleShutdown() {
136 session_write_close();
137 }
138 }