Redis processes the session based on redis. Redis processes the session based on redis. redis processes the session based on redis as follows. 1? Php2classSession_custom {3 private $ redis; redis instance's session processing method based on redis, redis processes session
A redis-based session processing method is as follows.
1
Redis = new Redis (); 9 return $ this-> redis-> connect ("127.0.0.1", 6379); 10} 11 12 // this method is called when the session ends, close redis connection 13 public function close () {14 $ this-> redis-> close (); 15 return true; 16} 17 18 // this method is called when the session saves data, call 19 public function write ($ session_id, $ data) {20 return $ this-> redis-> hMSet ($ this-> prefix. $ session_id, array ('expires' => time (), 'data' => $ data); 21} 22 23 // start the session automatically or By calling the session_start () function to manually start a session, PHP calls the read callback function internally to obtain session data. 24 public function read ($ session_id) {25 if ($ this-> redis-> exists ($ this-> prefix. $ session_id) {26 return $ this-> redis-> hGet ($ this-> prefix. $ session_id, 'data'); 27} 28 return ''; 29} 30 31 // clear session data. when you call the session_destroy () function, or call session_regenerate_id () this callback function is called when the destroy parameter is set to TRUE. 32 public function destroy ($ session_id) {33 if ($ this-> redis-> exists ($ this-> prefix. $ session_id) {34 return $ this-> redis-> del ($ this-> prefix. $ session_id)> 0? True: false; 35} 36 return true; 37} 38 39 // the garbage collection function. The Call cycle is set to session. gc_probability and session. gc_pisor parameter control 40 public function gc ($ maxlifetime) {41 $ allKeys = $ this-> redis-> keys ("{$ this-> prefix }*"); 42 foreach ($ allKeys as $ key) {43 if ($ this-> redis-> exists ($ key) & $ this-> redis-> hGet ($ key, 'expires') + $ maxlifetime <time () {44 $ this-> redis-> del ($ key); 45} 46} 47 return true; 48} 49} 50 51 // call the custom session processing method 52 $ handler = new Session_custom (); 53 session_set_save_handler (54 array ($ handler, 'open '), 55 array ($ handler, 'close'), 56 array ($ handler, 'read'), 57 array ($ handler, 'write'), 58 array ($ handler, 'deststroy'), 59 array ($ handler, 'gc ') 60 ); 61 62 // the following line of code prevents unexpected behavior that may occur when an object is used as the session storage manager, indicating that after the script is executed or after exit () is called, store the current session data and close the current session 63 register_shutdown_function ('session _ write_close '); 64 65 session_start (); 66 67 // you can use the session
Supplement:
The session. gc_probability and session. gc_pisor configuration options in the php. ini file jointly determine the gc function call time. The default values are 1 and 1000, indicating that each request has only 1/1000 chance to call the gc function.
The following describes a redis-based session processing method. 1? Php 2 class Session_custom {3 private $ redis; // redis instance...