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

Wincache::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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