Passed
Push — 6.0 ( f409e2...e26961 )
by liu
02:35
created

Memcache::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\session\driver;
14
15
use think\Exception;
16
use think\session\SessionHandler;
17
18
class Memcache implements SessionHandler
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
19
{
20
    protected $handler = null;
21
    protected $config  = [
22
        'host'       => '127.0.0.1', // memcache主机
23
        'port'       => 11211, // memcache端口
24
        'expire'     => 3600, // session有效期
25
        'timeout'    => 0, // 连接超时时间(单位:毫秒)
26
        'persistent' => true, // 长连接
27
        'prefix'     => '', // session name (memcache key前缀)
28
    ];
29
30
    public function __construct(array $config = [])
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
31
    {
32
        $this->config = array_merge($this->config, $config);
33
34
        $this->init();
35
    }
36
37
    /**
38
     * 打开Session
39
     * @access public
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41
    public function init(): bool
42
    {
43
        // 检测php环境
44
        if (!extension_loaded('memcache')) {
45
            throw new Exception('not support:memcache');
46
        }
47
48
        $this->handler = new \Memcache;
49
50
        // 支持集群
51
        $hosts = explode(',', $this->config['host']);
52
        $ports = explode(',', $this->config['port']);
53
54
        if (empty($ports[0])) {
55
            $ports[0] = 11211;
56
        }
57
58
        // 建立连接
59
        foreach ($hosts as $i => $host) {
60
            $port = $ports[$i] ?? $ports[0];
61
            $this->config['timeout'] > 0 ?
62
            $this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']) :
0 ignored issues
show
Bug introduced by
$port of type string is incompatible with the type integer expected by parameter $port of MemcachePool::addServer(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            $this->handler->addServer($host, /** @scrutinizer ignore-type */ $port, $this->config['persistent'], 1, $this->config['timeout']) :
Loading history...
63
            $this->handler->addServer($host, $port, $this->config['persistent'], 1);
64
        }
65
66
        return true;
67
    }
68
69
    /**
70
     * 读取Session
71
     * @access public
72
     * @param  string $sessID
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     * @return array
74
     */
75
    public function read(string $sessID): array
76
    {
77
        return $this->handler->get($this->config['prefix'] . $sessID);
78
    }
79
80
    /**
81
     * 写入Session
82
     * @access public
83
     * @param  string $sessID
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param  array  $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     * @return array
86
     */
87
    public function write(string $sessID, array $data): bool
88
    {
89
        return $this->handler->set($this->config['prefix'] . $sessID, $data, 0, $this->config['expire']);
90
    }
91
92
    /**
93
     * 删除Session
94
     * @access public
95
     * @param  string $sessID
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
96
     * @return array
97
     */
98
    public function delete(string $sessID): bool
99
    {
100
        return $this->handler->delete($this->config['prefix'] . $sessID);
101
    }
102
103
}
104