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

SimpleCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 119
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setMultiple() 0 11 3
A deleteMultiple() 0 11 3
A getMultiple() 0 9 2
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\cache;
14
15
use Psr\SimpleCache\CacheInterface;
16
use think\exception\InvalidArgumentException;
17
18
/**
19
 * SimpleCache接口
20
 */
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...
21
abstract class SimpleCache implements CacheInterface
22
{
23
    /**
24
     * 判断缓存是否存在
25
     * @access public
26
     * @param  string $name 缓存变量名
27
     * @return bool
28
     */
29
    abstract public function has($name): bool;
30
31
    /**
32
     * 读取缓存
33
     * @access public
34
     * @param  string $name 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
35
     * @param  mixed  $default 默认值
36
     * @return mixed
37
     */
38
    abstract public function get($name, $default = false);
39
40
    /**
41
     * 写入缓存
42
     * @access public
43
     * @param  string                 $name 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
44
     * @param  mixed                  $value  存储数据
45
     * @param  null|int|\DateInterval $expire  有效时间 0为永久
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
46
     * @return bool
47
     */
48
    abstract public function set($name, $value, $expire = null): bool;
49
50
    /**
51
     * 自增缓存(针对数值缓存)
52
     * @access public
53
     * @param  string $name 缓存变量名
54
     * @param  int    $step 步长
55
     * @return false|int
56
     */
57
    abstract public function inc(string $name, int $step = 1);
58
59
    /**
60
     * 自减缓存(针对数值缓存)
61
     * @access public
62
     * @param  string $name 缓存变量名
63
     * @param  int    $step 步长
64
     * @return false|int
65
     */
66
    abstract public function dec(string $name, int $step = 1);
67
68
    /**
69
     * 删除缓存
70
     * @access public
71
     * @param  string $name 缓存变量名
72
     * @return bool
73
     */
74
    abstract public function delete($name): bool;
75
76
    /**
77
     * 清除缓存
78
     * @access public
79
     * @return bool
80
     */
81
    abstract public function clear(): bool;
82
83
    /**
84
     * 读取缓存
85
     * @access public
86
     * @param  iterable $keys 缓存变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
87
     * @param  mixed    $default 默认值
88
     * @return iterable
89
     * @throws InvalidArgumentException
90
     */
91
    public function getMultiple($keys, $default = null): iterable
92
    {
93
        $result = [];
94
95
        foreach ($keys as $key) {
96
            $result[$key] = $this->get($key, $default);
97
        }
98
99
        return $result;
100
    }
101
102
    /**
103
     * 写入缓存
104
     * @access public
105
     * @param  iterable                 $values 缓存数据
106
     * @param  null|int|\DateInterval   $ttl    有效时间 0为永久
107
     * @return bool
108
     */
109
    public function setMultiple($values, $ttl = null): bool
110
    {
111
        foreach ($values as $key => $val) {
112
            $result = $this->set($key, $val, $ttl);
113
114
            if (false === $result) {
115
                return false;
116
            }
117
        }
118
119
        return true;
120
    }
121
122
    /**
123
     * 删除缓存
124
     * @access public
125
     * @param iterable $keys 缓存变量名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
126
     * @return bool
127
     * @throws InvalidArgumentException
128
     */
129
    public function deleteMultiple($keys): bool
130
    {
131
        foreach ($keys as $key) {
132
            $result = $this->delete($key);
133
134
            if (false === $result) {
135
                return false;
136
            }
137
        }
138
139
        return true;
140
    }
141
}
142