Completed
Push — 6.0 ( a4ef3c...64a49e )
by yun
04:55
created

Cache::resolveConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
declare (strict_types = 1);
12
13
namespace think;
14
15
use Psr\SimpleCache\CacheInterface;
16
use think\cache\Driver;
17
use think\exception\InvalidArgumentException;
18
use think\helper\Arr;
19
20
/**
21
 * 缓存管理类
22
 * @mixin Driver
23
 * @mixin \think\cache\driver\File
24
 */
25
class Cache extends Manager implements CacheInterface
26
{
27
28
    protected $namespace = '\\think\\cache\\driver\\';
29
30
    /**
31
     * 默认驱动
32
     * @return array|string
33
     */
34 2
    public function getDefaultDriver(): string
35
    {
36 2
        return $this->getConfig('default');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getConfig('default') could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
37
    }
38
39
    /**
40
     * 获取缓存配置
41
     * @access public
42
     * @param null|string $name    名称
43
     * @param mixed       $default 默认值
44
     * @return mixed
45
     */
46 4
    public function getConfig(string $name = null, $default = null)
47
    {
48 4
        if (!is_null($name)) {
49 4
            return $this->app->config->get('cache.' . $name, $default);
50
        }
51
52 1
        return $this->app->config->get('cache');
53
    }
54
55
    /**
56
     * 获取驱动配置
57
     * @param string $store
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
59
     * @param null   $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @return array
61
     */
62 4
    public function getStoreConfig(string $store, string $name = null, $default = null)
63
    {
64 4
        if ($config = $this->getConfig("stores.{$store}")) {
65 3
            return Arr::get($config, $name, $default);
66
        }
67
68 1
        throw new \InvalidArgumentException("Store [$store] not found.");
69
    }
70
71 3
    protected function resolveType(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveType()
Loading history...
72
    {
73 3
        return $this->getStoreConfig($name, 'type', 'file');
74
    }
75
76 3
    protected function resolveConfig(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveConfig()
Loading history...
77
    {
78 3
        return $this->getStoreConfig($name);
79
    }
80
81
    /**
82
     * 连接或者切换缓存
83
     * @access public
84
     * @param string $name 连接配置名
85
     * @return Driver
86
     */
87 3
    public function store(string $name = null): Driver
88
    {
89 3
        return $this->driver($name);
90
    }
91
92
    /**
93
     * 清空缓冲池
94
     * @access public
95
     * @return bool
96
     */
97 2
    public function clear(): bool
98
    {
99 2
        return $this->store()->clear();
100
    }
101
102
    /**
103
     * 读取缓存
104
     * @access public
105
     * @param string $key     缓存变量名
106
     * @param mixed  $default 默认值
107
     * @return mixed
108
     */
109 2
    public function get($key, $default = null)
110
    {
111 2
        return $this->store()->get($key, $default);
112
    }
113
114
    /**
115
     * 写入缓存
116
     * @access public
117
     * @param string        $key   缓存变量名
118
     * @param mixed         $value 存储数据
119
     * @param int|\DateTime $ttl   有效时间 0为永久
120
     * @return bool
121
     */
122 2
    public function set($key, $value, $ttl = null): bool
123
    {
124 2
        return $this->store()->set($key, $value, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl 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

124
        return $this->store()->set($key, $value, /** @scrutinizer ignore-type */ $ttl);
Loading history...
125
    }
126
127
    /**
128
     * 删除缓存
129
     * @access public
130
     * @param string $key 缓存变量名
131
     * @return bool
132
     */
133 2
    public function delete($key): bool
134
    {
135 2
        return $this->store()->delete($key);
136
    }
137
138
    /**
139
     * 读取缓存
140
     * @access public
141
     * @param iterable $keys    缓存变量名
142
     * @param mixed    $default 默认值
143
     * @return iterable
144
     * @throws InvalidArgumentException
145
     */
146
    public function getMultiple($keys, $default = null): iterable
147
    {
148
        return $this->store()->getMultiple($keys, $default);
149
    }
150
151
    /**
152
     * 写入缓存
153
     * @access public
154
     * @param iterable               $values 缓存数据
155
     * @param null|int|\DateInterval $ttl    有效时间 0为永久
156
     * @return bool
157
     */
158
    public function setMultiple($values, $ttl = null): bool
159
    {
160
        return $this->store()->setMultiple($values, $ttl);
161
    }
162
163
    /**
164
     * 删除缓存
165
     * @access public
166
     * @param iterable $keys 缓存变量名
167
     * @return bool
168
     * @throws InvalidArgumentException
169
     */
170
    public function deleteMultiple($keys): bool
171
    {
172
        return $this->store()->deleteMultiple($keys);
173
    }
174
175
    /**
176
     * 判断缓存是否存在
177
     * @access public
178
     * @param string $key 缓存变量名
179
     * @return bool
180
     */
181 1
    public function has($key): bool
182
    {
183 1
        return $this->store()->has($key);
184
    }
185
186
}
187