Completed
Push — 6.0 ( 28cc55...ecaa46 )
by liu
03:26
created

Memcached::inc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
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
12
namespace think\cache\driver;
13
14
use think\cache\Driver;
15
use think\contract\CacheHandlerInterface;
16
17
/**
18
 * Memcached缓存类
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
class Memcached extends Driver implements CacheHandlerInterface
21
{
22
    /**
23
     * 配置参数
24
     * @var array
25
     */
26
    protected $options = [
27
        'host'       => '127.0.0.1',
28
        'port'       => 11211,
29
        'expire'     => 0,
30
        'timeout'    => 0, // 超时时间(单位:毫秒)
31
        'prefix'     => '',
32
        'username'   => '', //账号
33
        'password'   => '', //密码
34
        'option'     => [],
35
        'tag_prefix' => 'tag_',
36
        'serialize'  => [],
37
    ];
38
39
    /**
40
     * 架构函数
41
     * @access public
42
     * @param  array $options 缓存参数
43
     */
44
    public function __construct(array $options = [])
45
    {
46
        if (!extension_loaded('memcached')) {
47
            throw new \BadFunctionCallException('not support: memcached');
48
        }
49
50
        if (!empty($options)) {
51
            $this->options = array_merge($this->options, $options);
52
        }
53
54
        $this->handler = new \Memcached;
55
56
        if (!empty($this->options['option'])) {
57
            $this->handler->setOptions($this->options['option']);
58
        }
59
60
        // 设置连接超时时间(单位:毫秒)
61
        if ($this->options['timeout'] > 0) {
62
            $this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
63
        }
64
65
        // 支持集群
66
        $hosts = (array) $this->options['host'];
67
        $ports = (array) $this->options['port'];
68
        if (empty($ports[0])) {
69
            $ports[0] = 11211;
70
        }
71
72
        // 建立连接
73
        $servers = [];
74
        foreach ($hosts as $i => $host) {
75
            $servers[] = [$host, $ports[$i] ?? $ports[0], 1];
76
        }
77
78
        $this->handler->addServers($servers);
79
80
        if ('' != $this->options['username']) {
81
            $this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
82
            $this->handler->setSaslAuthData($this->options['username'], $this->options['password']);
83
        }
84
    }
85
86
    /**
87
     * 判断缓存
88
     * @access public
89
     * @param  string $name 缓存变量名
90
     * @return bool
91
     */
92
    public function has($name): bool
93
    {
94
        $key = $this->getCacheKey($name);
95
96
        return $this->handler->get($key) ? true : false;
97
    }
98
99
    /**
100
     * 读取缓存
101
     * @access public
102
     * @param  string $name 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
103
     * @param  mixed  $default 默认值
104
     * @return mixed
105
     */
106
    public function get($name, $default = false)
107
    {
108
        $this->readTimes++;
109
110
        $result = $this->handler->get($this->getCacheKey($name));
111
112
        return false !== $result ? $this->unserialize($result) : $default;
113
    }
114
115
    /**
116
     * 写入缓存
117
     * @access public
118
     * @param  string            $name 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
119
     * @param  mixed             $value  存储数据
120
     * @param  integer|\DateTime $expire  有效时间(秒)
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
121
     * @return bool
122
     */
123
    public function set($name, $value, $expire = null): bool
124
    {
125
        $this->writeTimes++;
126
127
        if (is_null($expire)) {
128
            $expire = $this->options['expire'];
129
        }
130
131
        $key    = $this->getCacheKey($name);
132
        $expire = $this->getExpireTime($expire);
133
        $value  = $this->serialize($value);
134
135
        if ($this->handler->set($key, $value, $expire)) {
136
            return true;
137
        }
138
139
        return false;
140
    }
141
142
    /**
143
     * 自增缓存(针对数值缓存)
144
     * @access public
145
     * @param  string $name 缓存变量名
146
     * @param  int    $step 步长
147
     * @return false|int
148
     */
149
    public function inc(string $name, int $step = 1)
150
    {
151
        $this->writeTimes++;
152
153
        $key = $this->getCacheKey($name);
154
155
        if ($this->handler->get($key)) {
156
            return $this->handler->increment($key, $step);
157
        }
158
159
        return $this->handler->set($key, $step);
160
    }
161
162
    /**
163
     * 自减缓存(针对数值缓存)
164
     * @access public
165
     * @param  string $name 缓存变量名
166
     * @param  int    $step 步长
167
     * @return false|int
168
     */
169
    public function dec(string $name, int $step = 1)
170
    {
171
        $this->writeTimes++;
172
173
        $key   = $this->getCacheKey($name);
174
        $value = $this->handler->get($key) - $step;
175
        $res   = $this->handler->set($key, $value);
176
177
        return !$res ? false : $value;
178
    }
179
180
    /**
181
     * 删除缓存
182
     * @access public
183
     * @param  string       $name 缓存变量名
184
     * @param  bool|false   $ttl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
185
     * @return bool
186
     */
187
    public function delete($name, $ttl = false): bool
188
    {
189
        $this->writeTimes++;
190
191
        $key = $this->getCacheKey($name);
192
193
        return false === $ttl ?
194
        $this->handler->delete($key) :
195
        $this->handler->delete($key, $ttl);
196
    }
197
198
    /**
199
     * 清除缓存
200
     * @access public
201
     * @return bool
202
     */
203
    public function clear(): bool
204
    {
205
        $this->writeTimes++;
206
207
        return $this->handler->flush();
208
    }
209
210
    /**
211
     * 删除缓存标签
212
     * @access public
213
     * @param  array $keys 缓存标识列表
214
     * @return void
215
     */
216
    public function clearTag(array $keys): void
217
    {
218
        $this->handler->deleteMulti($keys);
219
    }
220
221
}
222