Issues (183)

src/think/Cache.php (2 issues)

1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 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\cache\TagSet;
18
use think\exception\InvalidArgumentException;
19
use think\helper\Arr;
20
21
/**
22
 * 缓存管理类
23
 * @mixin Driver
24
 * @mixin \think\cache\driver\File
25
 */
26
class Cache extends Manager implements CacheInterface
27
{
28
29
    protected $namespace = '\\think\\cache\\driver\\';
30
31
    /**
32
     * 默认驱动
33
     * @return string|null
34
     */
35 3
    public function getDefaultDriver()
36
    {
37 3
        return $this->getConfig('default');
38
    }
39
40
    /**
41
     * 获取缓存配置
42
     * @access public
43
     * @param null|string $name    名称
44
     * @param mixed       $default 默认值
45
     * @return mixed
46
     */
47 9
    public function getConfig(string $name = null, $default = null)
48
    {
49 9
        if (!is_null($name)) {
50 9
            return $this->app->config->get('cache.' . $name, $default);
51
        }
52
53 3
        return $this->app->config->get('cache');
54
    }
55
56
    /**
57
     * 获取驱动配置
58
     * @param string $store
59
     * @param string $name
60
     * @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...
61
     * @return array
62
     */
63 9
    public function getStoreConfig(string $store, string $name = null, $default = null)
64
    {
65 9
        if ($config = $this->getConfig("stores.{$store}")) {
66 6
            return Arr::get($config, $name, $default);
67
        }
68
69 3
        throw new \InvalidArgumentException("Store [$store] not found.");
70
    }
71
72 6
    protected function resolveType(string $name)
73
    {
74 6
        return $this->getStoreConfig($name, 'type', 'file');
75
    }
76
77 6
    protected function resolveConfig(string $name)
78
    {
79 6
        return $this->getStoreConfig($name);
80
    }
81
82
    /**
83
     * 连接或者切换缓存
84
     * @access public
85
     * @param string $name 连接配置名
86
     * @return Driver
87
     */
88 6
    public function store(string $name = null)
89
    {
90 6
        return $this->driver($name);
91
    }
92
93
    /**
94
     * 清空缓冲池
95
     * @access public
96
     * @return bool
97
     */
98 3
    public function clear(): bool
99
    {
100 3
        return $this->store()->clear();
101
    }
102
103
    /**
104
     * 读取缓存
105
     * @access public
106
     * @param string $key     缓存变量名
107
     * @param mixed  $default 默认值
108
     * @return mixed
109
     */
110 3
    public function get($key, $default = null)
111
    {
112 3
        return $this->store()->get($key, $default);
113
    }
114
115
    /**
116
     * 写入缓存
117
     * @access public
118
     * @param string        $key   缓存变量名
119
     * @param mixed         $value 存储数据
120
     * @param int|\DateTime $ttl   有效时间 0为永久
121
     * @return bool
122
     */
123 3
    public function set($key, $value, $ttl = null): bool
124
    {
125 3
        return $this->store()->set($key, $value, $ttl);
0 ignored issues
show
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

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