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

Wincache::rm()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
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
 * Wincache缓存驱动
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 Wincache extends Driver implements CacheHandlerInterface
21
{
22
    /**
23
     * 配置参数
24
     * @var array
25
     */
26
    protected $options = [
27
        'prefix'     => '',
28
        'expire'     => 0,
29
        'tag_prefix' => 'tag_',
30
        'serialize'  => [],
31
    ];
32
33
    /**
34
     * 架构函数
35
     * @access public
36
     * @param  array $options 缓存参数
37
     * @throws \BadFunctionCallException
38
     */
39
    public function __construct(array $options = [])
40
    {
41
        if (!function_exists('wincache_ucache_info')) {
42
            throw new \BadFunctionCallException('not support: WinCache');
43
        }
44
45
        if (!empty($options)) {
46
            $this->options = array_merge($this->options, $options);
47
        }
48
    }
49
50
    /**
51
     * 判断缓存
52
     * @access public
53
     * @param  string $name 缓存变量名
54
     * @return bool
55
     */
56
    public function has($name): bool
57
    {
58
        $this->readTimes++;
59
60
        $key = $this->getCacheKey($name);
61
62
        return wincache_ucache_exists($key);
63
    }
64
65
    /**
66
     * 读取缓存
67
     * @access public
68
     * @param  string $name 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
69
     * @param  mixed  $default 默认值
70
     * @return mixed
71
     */
72
    public function get($name, $default = false)
73
    {
74
        $this->readTimes++;
75
76
        $key = $this->getCacheKey($name);
77
78
        return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
79
    }
80
81
    /**
82
     * 写入缓存
83
     * @access public
84
     * @param  string            $name 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
85
     * @param  mixed             $value  存储数据
86
     * @param  integer|\DateTime $expire  有效时间(秒)
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
87
     * @return bool
88
     */
89
    public function set($name, $value, $expire = null): bool
90
    {
91
        $this->writeTimes++;
92
93
        if (is_null($expire)) {
94
            $expire = $this->options['expire'];
95
        }
96
97
        $key    = $this->getCacheKey($name);
98
        $expire = $this->getExpireTime($expire);
99
        $value  = $this->serialize($value);
100
101
        if (wincache_ucache_set($key, $value, $expire)) {
102
            return true;
103
        }
104
105
        return false;
106
    }
107
108
    /**
109
     * 自增缓存(针对数值缓存)
110
     * @access public
111
     * @param  string    $name 缓存变量名
112
     * @param  int       $step 步长
113
     * @return false|int
114
     */
115
    public function inc(string $name, int $step = 1)
116
    {
117
        $this->writeTimes++;
118
119
        $key = $this->getCacheKey($name);
120
121
        return wincache_ucache_inc($key, $step);
122
    }
123
124
    /**
125
     * 自减缓存(针对数值缓存)
126
     * @access public
127
     * @param  string    $name 缓存变量名
128
     * @param  int       $step 步长
129
     * @return false|int
130
     */
131
    public function dec(string $name, int $step = 1)
132
    {
133
        $this->writeTimes++;
134
135
        $key = $this->getCacheKey($name);
136
137
        return wincache_ucache_dec($key, $step);
138
    }
139
140
    /**
141
     * 删除缓存
142
     * @access public
143
     * @param  string $name 缓存变量名
144
     * @return bool
145
     */
146
    public function delete($name): bool
147
    {
148
        $this->writeTimes++;
149
150
        return wincache_ucache_delete($this->getCacheKey($name));
151
    }
152
153
    /**
154
     * 清除缓存
155
     * @access public
156
     * @return bool
157
     */
158
    public function clear(): bool
159
    {
160
        $this->writeTimes++;
161
        return wincache_ucache_clear();
162
    }
163
164
    /**
165
     * 删除缓存标签
166
     * @access public
167
     * @param  array $keys 缓存标识列表
168
     * @return void
169
     */
170
    public function clearTag(array $keys): void
171
    {
172
        wincache_ucache_delete($keys);
173
    }
174
175
}
176