RedisClusterSessionHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. use Workerman\Protocols\Http\Session;
  16. class RedisClusterSessionHandler extends RedisSessionHandler
  17. {
  18. public function __construct($config)
  19. {
  20. $this->_maxLifetime = (int)Session::$lifetime;
  21. $timeout = $config['timeout'] ?? 2;
  22. $read_timeout = $config['read_timeout'] ?? $timeout;
  23. $persistent = $config['persistent'] ?? false;
  24. $auth = $config['auth'] ?? '';
  25. $args = [null, $config['host'], $timeout, $read_timeout, $persistent];
  26. if ($auth) {
  27. $args[] = $auth;
  28. }
  29. $this->_redis = new \RedisCluster(...$args);
  30. if (empty($config['prefix'])) {
  31. $config['prefix'] = 'redis_session_';
  32. }
  33. $this->_redis->setOption(\Redis::OPT_PREFIX, $config['prefix']);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function read($session_id)
  39. {
  40. return $this->_redis->get($session_id);
  41. }
  42. }