1 | <?php |
||
29 | abstract class Driver implements CacheInterface, CacheHandlerInterface |
||
30 | { |
||
31 | /** |
||
32 | * 驱动句柄 |
||
33 | * @var object |
||
34 | */ |
||
35 | protected $handler = null; |
||
36 | |||
37 | /** |
||
38 | * 缓存读取次数 |
||
39 | * @var integer |
||
40 | */ |
||
41 | protected $readTimes = 0; |
||
42 | |||
43 | /** |
||
44 | * 缓存写入次数 |
||
45 | * @var integer |
||
46 | */ |
||
47 | protected $writeTimes = 0; |
||
48 | |||
49 | /** |
||
50 | * 缓存参数 |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $options = []; |
||
54 | |||
55 | /** |
||
56 | * 缓存标签 |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $tag = []; |
||
60 | |||
61 | /** |
||
62 | * 获取有效期 |
||
63 | * @access protected |
||
64 | * @param integer|DateTimeInterface|DateInterval $expire 有效期 |
||
65 | * @return int |
||
66 | */ |
||
67 | 3 | protected function getExpireTime($expire): int |
|
79 | |||
80 | /** |
||
81 | * 获取实际的缓存标识 |
||
82 | * @access public |
||
83 | * @param string $name 缓存名 |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getCacheKey(string $name): string |
||
90 | |||
91 | /** |
||
92 | * 读取缓存并删除 |
||
93 | * @access public |
||
94 | * @param string $name 缓存变量名 |
||
95 | * @return mixed |
||
96 | */ |
||
97 | public function pull(string $name) |
||
106 | |||
107 | /** |
||
108 | * 追加(数组)缓存 |
||
109 | * @access public |
||
110 | * @param string $name 缓存变量名 |
||
111 | * @param mixed $value 存储数据 |
||
112 | * @return void |
||
113 | */ |
||
114 | 3 | public function push(string $name, $value): void |
|
132 | |||
133 | /** |
||
134 | * 如果不存在则写入缓存 |
||
135 | * @access public |
||
136 | * @param string $name 缓存变量名 |
||
137 | * @param mixed $value 存储数据 |
||
138 | * @param int $expire 有效时间 0为永久 |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function remember(string $name, $value, $expire = null) |
||
175 | |||
176 | /** |
||
177 | * 缓存标签 |
||
178 | * @access public |
||
179 | * @param string|array $name 标签名 |
||
180 | * @return TagSet |
||
181 | */ |
||
182 | 3 | public function tag($name): TagSet |
|
196 | |||
197 | /** |
||
198 | * 获取标签包含的缓存标识 |
||
199 | * @access public |
||
200 | * @param string $tag 标签标识 |
||
201 | * @return array |
||
202 | */ |
||
203 | 3 | public function getTagItems(string $tag): array |
|
207 | |||
208 | /** |
||
209 | * 获取实际标签名 |
||
210 | * @access public |
||
211 | * @param string $tag 标签名 |
||
212 | * @return string |
||
213 | */ |
||
214 | 3 | public function getTagKey(string $tag): string |
|
218 | |||
219 | /** |
||
220 | * 序列化数据 |
||
221 | * @access protected |
||
222 | * @param mixed $data 缓存数据 |
||
223 | * @return string |
||
224 | */ |
||
225 | 3 | protected function serialize($data): string |
|
235 | |||
236 | /** |
||
237 | * 反序列化数据 |
||
238 | * @access protected |
||
239 | * @param string $data 缓存数据 |
||
240 | * @return mixed |
||
241 | */ |
||
242 | 3 | protected function unserialize(string $data) |
|
252 | |||
253 | /** |
||
254 | * 返回句柄对象,可执行其它高级方法 |
||
255 | * |
||
256 | * @access public |
||
257 | * @return object |
||
258 | */ |
||
259 | public function handler() |
||
263 | |||
264 | /** |
||
265 | * 返回缓存读取次数 |
||
266 | * @access public |
||
267 | * @return int |
||
268 | */ |
||
269 | public function getReadTimes(): int |
||
273 | |||
274 | /** |
||
275 | * 返回缓存写入次数 |
||
276 | * @access public |
||
277 | * @return int |
||
278 | */ |
||
279 | public function getWriteTimes(): int |
||
283 | |||
284 | /** |
||
285 | * 读取缓存 |
||
286 | * @access public |
||
287 | * @param iterable $keys 缓存变量名 |
||
288 | * @param mixed $default 默认值 |
||
289 | * @return iterable |
||
290 | * @throws InvalidArgumentException |
||
291 | */ |
||
292 | 3 | public function getMultiple($keys, $default = null): iterable |
|
302 | |||
303 | /** |
||
304 | * 写入缓存 |
||
305 | * @access public |
||
306 | * @param iterable $values 缓存数据 |
||
307 | * @param null|int|\DateInterval $ttl 有效时间 0为永久 |
||
308 | * @return bool |
||
309 | */ |
||
310 | 3 | public function setMultiple($values, $ttl = null): bool |
|
322 | |||
323 | /** |
||
324 | * 删除缓存 |
||
325 | * @access public |
||
326 | * @param iterable $keys 缓存变量名 |
||
327 | * @return bool |
||
328 | * @throws InvalidArgumentException |
||
329 | */ |
||
330 | 3 | public function deleteMultiple($keys): bool |
|
342 | |||
343 | public function __call($method, $args) |
||
347 | } |
||
348 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.