Completed
Push — 6.0 ( d6c8d7...cb8a0b )
by liu
05:58
created

Memcache::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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