Completed
Push — 6.0 ( 1e69d2...64aa8b )
by yun
02:11
created

TagSet   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 67.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 113
ccs 21
cts 31
cp 0.6774
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 7 1
A clear() 0 12 2
A setMultiple() 0 11 3
A remember() 0 7 1
A append() 0 7 2
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
declare (strict_types = 1);
12
13
namespace think\cache;
14
15
/**
16
 * 标签集合
17
 */
18
class TagSet
19
{
20
    /**
21
     * 标签的缓存Key
22
     * @var array
23
     */
24
    protected $tag;
25
26
    /**
27
     * 缓存句柄
28
     * @var Driver
29
     */
30
    protected $handler;
31
32
    /**
33
     * 架构函数
34
     * @access public
35
     * @param array  $tag   缓存标签
36
     * @param Driver $cache 缓存对象
37
     */
38 3
    public function __construct(array $tag, Driver $cache)
39
    {
40 3
        $this->tag     = $tag;
41 3
        $this->handler = $cache;
42 3
    }
43
44
    /**
45
     * 写入缓存
46
     * @access public
47
     * @param string            $name   缓存变量名
48
     * @param mixed             $value  存储数据
49
     * @param integer|\DateTime $expire 有效时间(秒)
50
     * @return bool
51
     */
52 3
    public function set(string $name, $value, $expire = null): bool
53
    {
54 3
        $this->handler->set($name, $value, $expire);
0 ignored issues
show
Bug introduced by
It seems like $expire can also be of type DateTime; however, parameter $ttl of Psr\SimpleCache\CacheInterface::set() does only seem to accept DateInterval|integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $this->handler->set($name, $value, /** @scrutinizer ignore-type */ $expire);
Loading history...
55
56 3
        $this->append($name);
57
58 3
        return true;
59
    }
60
61
    /**
62
     * 追加缓存标识到标签
63
     * @access public
64
     * @param string $name 缓存变量名
65
     * @return void
66
     */
67 3
    public function append(string $name): void
68
    {
69 3
        $name = $this->handler->getCacheKey($name);
70
71 3
        foreach ($this->tag as $tag) {
72 3
            $key = $this->handler->getTagKey($tag);
73 3
            $this->handler->push($key, $name);
74
        }
75 3
    }
76
77
    /**
78
     * 写入缓存
79
     * @access public
80
     * @param iterable               $values 缓存数据
81
     * @param null|int|\DateInterval $ttl    有效时间 0为永久
82
     * @return bool
83
     */
84
    public function setMultiple($values, $ttl = null): bool
85
    {
86
        foreach ($values as $key => $val) {
87
            $result = $this->set($key, $val, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl can also be of type DateInterval; however, parameter $expire of think\cache\TagSet::set() does only seem to accept DateTime|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            $result = $this->set($key, $val, /** @scrutinizer ignore-type */ $ttl);
Loading history...
88
89
            if (false === $result) {
90
                return false;
91
            }
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * 如果不存在则写入缓存
99
     * @access public
100
     * @param string $name   缓存变量名
101
     * @param mixed  $value  存储数据
102
     * @param int    $expire 有效时间 0为永久
103
     * @return mixed
104
     */
105
    public function remember(string $name, $value, $expire = null)
106
    {
107
        $result = $this->handler->remember($name, $value, $expire);
108
109
        $this->append($name);
110
111
        return $result;
112
    }
113
114
    /**
115
     * 清除缓存
116
     * @access public
117
     * @return bool
118
     */
119 3
    public function clear(): bool
120
    {
121
        // 指定标签清除
122 3
        foreach ($this->tag as $tag) {
123 3
            $names = $this->handler->getTagItems($tag);
124 3
            $this->handler->clearTag($names);
125
126 3
            $key = $this->handler->getTagKey($tag);
127 3
            $this->handler->delete($key);
128
        }
129
130 3
        return true;
131
    }
132
}
133