PHP's method of processing session based on redis

PHP's method of processing session based on redis

 A redis-based method for processing sessions is as follows.

class Session_custom {

private $redis; // redis instance

private $prefix = 'sess_'; // session_id prefix

// When the session starts, this method will be executed to connect to the redis server

public function open($path, $name) {

$this-&http://gt;redis = new Redis();

return $this->redis->connect("127.0.0.1",6379);

}

// When the session ends, call this method to close the redis connection

public function close() {

$this->redis->close();

return true;

}

// This method is called when the session saves data, and it is called after the script is executed or the session_write_close method is called

public function write($session_id, $data) {

return $this->redis->hMSet($this->prefix.$session_id, array('expires' => time(), 'data' => $data));

}

// After automatically starting a session or manually starting a session by calling the session_start() function, PHP internally calls the read callback function to get session data.

public function read($session_id) {

if($this->redis->exists($this->prefix.$session_id)) {

return $this->redis->hGet($this->prefix.$session_id, 'data');

}

return '';

}

// Clear the data in the session. When the session_destroy() function is called, or the session_regenerate_id() function is called and the destroy parameter is set to TRUE, this callback function will be called.

public function destroy($session_id) {

if($this->redis->exists($this->prefix.$session_id)) {

return $this->redis->del($this->prefix.$session_id) > 0 ? true : false;

}

return true;

}

// Garbage collection function, the call cycle is controlled by the session.gc_probability and session.gc_divisor parameters

public function gc($maxlifetime) {

$allKeys = $this->redis->keys("{$this->prefix}*");

http:// foreach($allKeys as $key) {

if($this-&glaaxjksWPt;redis->exists($key) && $this->redis->hGet($key, 'expires') + $maxlifetime < time()) {

$this->redis->del($key);

}

}

return true;

}

}

// Call the custom session processing method

$handler = new Session_custom();

session_set_save_handler(

array($handler, 'open'),

array($handler, 'close'),

array($handler, 'read'),

array($handler, 'write'),

array($handler, 'destroy'),

array($handler, 'gc')

);

// The following line of code can prevent unexpected behavior that may be caused when using an object as a session save manager, indicating that after the script is executed or after calling exit(), the current session data is stored and the current session is closed

register_shutdown_function('session_write_close');

session_start();

// You can use the session

Replenish:

The session.gc_probability and session.gc_divisor configuration options in the php.ini file jointly determine the timing of the gc function call. The default value is divided into 1 and 1000, which means that each request has only a 1/1000 chance to call the gc function.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Guess you like

Origin blog.csdn.net/G171104/article/details/132038967