|
1
|
|
|
<?php |
|
2
|
|
|
namespace PhpBoot\Cache; |
|
3
|
|
|
use Doctrine\Common\Cache\Cache; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* 可检查缓存是否失效的缓存 |
|
7
|
|
|
* @author caoym |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class CheckableCache |
|
11
|
|
|
{ |
|
12
|
89 |
|
function __construct(Cache $impl){ |
|
|
|
|
|
|
13
|
89 |
|
$this->impl = $impl; |
|
14
|
89 |
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* 设置cache |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
* @param mixed $var |
|
21
|
|
|
* @param int |
|
22
|
|
|
* @param callable $expireCheck |
|
23
|
|
|
* @return boolean |
|
24
|
|
|
* 缓存过期检查方法, 缓存过期(超过ttl)后, get时调用, 返回true表示缓存继续可用. |
|
25
|
|
|
* 如checker($got_var, $time) |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
21 |
|
public function set($name, $var, $ttl = 0, callable $expireCheck = null) |
|
29
|
|
|
{ |
|
30
|
21 |
|
$res = $this->impl->save($name, serialize(array( |
|
31
|
21 |
|
$var, |
|
32
|
21 |
|
$ttl, |
|
33
|
21 |
|
$expireCheck, |
|
34
|
21 |
|
time(), |
|
35
|
21 |
|
)), is_null($expireCheck) ? $ttl : 0); |
|
36
|
21 |
|
return $res; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
* @param mixed|null $default |
|
42
|
|
|
* @param mixed|null $expiredData |
|
43
|
|
|
* @param bool $deleteExpiredData |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
89 |
|
public function get($name, $default = null, &$expiredData=null, $deleteExpiredData=true) |
|
47
|
|
|
{ |
|
48
|
89 |
|
$expiredData = null; |
|
49
|
89 |
|
$res = $this->impl->fetch($name); |
|
50
|
89 |
|
if ($res) { |
|
51
|
74 |
|
list ($data, $ttl, $checker, $createdTime) = unserialize($res); |
|
52
|
|
|
// 如果指定了checker, ttl代表每次检查的间隔时间, 0表示每次get都需要经过checker检查 |
|
53
|
|
|
// 如果没有指定checker, ttl表示缓存过期时间, 为0表示永不过期 |
|
54
|
74 |
|
if ($checker !== null) { |
|
55
|
74 |
|
if ($ttl == 0 || ($createdTime + $ttl < time())) { |
|
56
|
74 |
|
$valid = $checker($data, $createdTime); |
|
57
|
74 |
|
if(!$valid){ |
|
58
|
|
|
$expiredData = $data; |
|
59
|
|
|
if($deleteExpiredData){ |
|
60
|
|
|
$this->impl->delete($name); |
|
61
|
|
|
} |
|
62
|
|
|
return $default; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
74 |
|
} |
|
66
|
74 |
|
}else if ($ttl != 0 && ($createdTime + $ttl < time())) { |
|
67
|
|
|
$this->impl->delete($name); |
|
68
|
|
|
return $default; |
|
69
|
|
|
} |
|
70
|
74 |
|
return $data; |
|
71
|
|
|
} |
|
72
|
21 |
|
return $default; |
|
73
|
|
|
} |
|
74
|
|
|
/** |
|
75
|
|
|
* 删除 |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
*/ |
|
78
|
|
|
public function del($name){ |
|
79
|
|
|
return $this->impl->delete($name); |
|
80
|
|
|
} |
|
81
|
|
|
public function getImpl() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->impl; |
|
84
|
|
|
} |
|
85
|
|
|
/** |
|
86
|
|
|
* @var Cache |
|
87
|
|
|
*/ |
|
88
|
|
|
private $impl; |
|
89
|
|
|
} |
|
90
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.