Blame view

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