Memcached.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\session\driver;
  12. use SessionHandler;
  13. use think\Exception;
  14. class Memcached extends SessionHandler
  15. {
  16. protected $handler = null;
  17. protected $config = [
  18. 'host' => '127.0.0.1', // memcache主机
  19. 'port' => 11211, // memcache端口
  20. 'expire' => 3600, // session有效期
  21. 'timeout' => 0, // 连接超时时间(单位:毫秒)
  22. 'session_name' => '', // memcache key前缀
  23. 'username' => '', //账号
  24. 'password' => '', //密码
  25. ];
  26. public function __construct($config = [])
  27. {
  28. $this->config = array_merge($this->config, $config);
  29. }
  30. /**
  31. * 打开Session
  32. * @access public
  33. * @param string $savePath
  34. * @param mixed $sessName
  35. */
  36. public function open($savePath, $sessName)
  37. {
  38. // 检测php环境
  39. if (!extension_loaded('memcached')) {
  40. throw new Exception('not support:memcached');
  41. }
  42. $this->handler = new \Memcached;
  43. // 设置连接超时时间(单位:毫秒)
  44. if ($this->config['timeout'] > 0) {
  45. $this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']);
  46. }
  47. // 支持集群
  48. $hosts = explode(',', $this->config['host']);
  49. $ports = explode(',', $this->config['port']);
  50. if (empty($ports[0])) {
  51. $ports[0] = 11211;
  52. }
  53. // 建立连接
  54. $servers = [];
  55. foreach ((array) $hosts as $i => $host) {
  56. $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
  57. }
  58. $this->handler->addServers($servers);
  59. if ('' != $this->config['username']) {
  60. $this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
  61. $this->handler->setSaslAuthData($this->config['username'], $this->config['password']);
  62. }
  63. return true;
  64. }
  65. /**
  66. * 关闭Session
  67. * @access public
  68. */
  69. public function close()
  70. {
  71. $this->gc(ini_get('session.gc_maxlifetime'));
  72. $this->handler->quit();
  73. $this->handler = null;
  74. return true;
  75. }
  76. /**
  77. * 读取Session
  78. * @access public
  79. * @param string $sessID
  80. */
  81. public function read($sessID)
  82. {
  83. return (string) $this->handler->get($this->config['session_name'] . $sessID);
  84. }
  85. /**
  86. * 写入Session
  87. * @access public
  88. * @param string $sessID
  89. * @param String $sessData
  90. * @return bool
  91. */
  92. public function write($sessID, $sessData)
  93. {
  94. return $this->handler->set($this->config['session_name'] . $sessID, $sessData, $this->config['expire']);
  95. }
  96. /**
  97. * 删除Session
  98. * @access public
  99. * @param string $sessID
  100. * @return bool
  101. */
  102. public function destroy($sessID)
  103. {
  104. return $this->handler->delete($this->config['session_name'] . $sessID);
  105. }
  106. /**
  107. * Session 垃圾回收
  108. * @access public
  109. * @param string $sessMaxLifeTime
  110. * @return true
  111. */
  112. public function gc($sessMaxLifeTime)
  113. {
  114. return true;
  115. }
  116. }