RedisSessionHandler.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Protocols\Http\Session;
  15. /**
  16. * Class RedisSessionHandler
  17. * @package Workerman\Protocols\Http\Session
  18. */
  19. class RedisSessionHandler extends \SessionHandler
  20. {
  21. /**
  22. * @var \Redis
  23. */
  24. protected $_redis;
  25. /**
  26. * @var int
  27. */
  28. protected $_maxLifeTime;
  29. /**
  30. * RedisSessionHandler constructor.
  31. * @param $config = [
  32. * 'host' => '127.0.0.1',
  33. * 'port' => 6379,
  34. * 'timeout' => 2,
  35. * 'auth' => '******',
  36. * 'database' => 2,
  37. * 'prefix' => 'redis_session_',
  38. * ]
  39. */
  40. public function __construct($config)
  41. {
  42. if (false === extension_loaded('redis')) {
  43. throw new \RuntimeException('Please install redis extension.');
  44. }
  45. $this->_maxLifeTime = (int)ini_get('session.gc_maxlifetime');
  46. if (!isset($config['timeout'])) {
  47. $config['timeout'] = 2;
  48. }
  49. $this->_redis = new \Redis();
  50. if (false === $this->_redis->connect($config['host'], $config['port'], $config['timeout'])) {
  51. throw new \RuntimeException("Redis connect {$config['host']}:{$config['port']} fail.");
  52. }
  53. if (!empty($config['auth'])) {
  54. $this->_redis->auth($config['auth']);
  55. }
  56. if (!empty($config['database'])) {
  57. $this->_redis->select($config['database']);
  58. }
  59. if (empty($config['prefix'])) {
  60. $config['prefix'] = 'redis_session_';
  61. }
  62. $this->_redis->setOption(\Redis::OPT_PREFIX, $config['prefix']);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function open($save_path, $name)
  68. {
  69. return true;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function read($session_id)
  75. {
  76. return $this->_redis->get($session_id);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function write($session_id, $session_data)
  82. {
  83. return true === $this->_redis->setex($session_id, $this->_maxLifeTime, $session_data);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function destroy($session_id)
  89. {
  90. $this->_redis->del($session_id);
  91. return true;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function close()
  97. {
  98. return true;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function gc($maxlifetime)
  104. {
  105. return true;
  106. }
  107. }