No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Memcache.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 SessionHandlerInterface;
  13. use think\Exception;
  14. class Memcache implements SessionHandlerInterface
  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. 'persistent' => true, // 长连接
  23. 'session_name' => '', // memcache key前缀
  24. ];
  25. public function __construct($config = [])
  26. {
  27. $this->config = array_merge($this->config, $config);
  28. }
  29. /**
  30. * 打开Session
  31. * @access public
  32. * @param string $savePath
  33. * @param mixed $sessName
  34. */
  35. public function open($savePath, $sessName)
  36. {
  37. // 检测php环境
  38. if (!extension_loaded('memcache')) {
  39. throw new Exception('not support:memcache');
  40. }
  41. $this->handler = new \Memcache;
  42. // 支持集群
  43. $hosts = explode(',', $this->config['host']);
  44. $ports = explode(',', $this->config['port']);
  45. if (empty($ports[0])) {
  46. $ports[0] = 11211;
  47. }
  48. // 建立连接
  49. foreach ((array) $hosts as $i => $host) {
  50. $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
  51. $this->config['timeout'] > 0 ?
  52. $this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']) :
  53. $this->handler->addServer($host, $port, $this->config['persistent'], 1);
  54. }
  55. return true;
  56. }
  57. /**
  58. * 关闭Session
  59. * @access public
  60. */
  61. public function close()
  62. {
  63. $this->gc(ini_get('session.gc_maxlifetime'));
  64. $this->handler->close();
  65. $this->handler = null;
  66. return true;
  67. }
  68. /**
  69. * 读取Session
  70. * @access public
  71. * @param string $sessID
  72. */
  73. public function read($sessID)
  74. {
  75. return (string) $this->handler->get($this->config['session_name'] . $sessID);
  76. }
  77. /**
  78. * 写入Session
  79. * @access public
  80. * @param string $sessID
  81. * @param string $sessData
  82. * @return bool
  83. */
  84. public function write($sessID, $sessData)
  85. {
  86. return $this->handler->set($this->config['session_name'] . $sessID, $sessData, 0, $this->config['expire']);
  87. }
  88. /**
  89. * 删除Session
  90. * @access public
  91. * @param string $sessID
  92. * @return bool
  93. */
  94. public function destroy($sessID)
  95. {
  96. return $this->handler->delete($this->config['session_name'] . $sessID);
  97. }
  98. /**
  99. * Session 垃圾回收
  100. * @access public
  101. * @param string $sessMaxLifeTime
  102. * @return true
  103. */
  104. public function gc($sessMaxLifeTime)
  105. {
  106. return true;
  107. }
  108. }