/
redisFPC.php
121 lines (114 loc) · 3.79 KB
/
redisFPC.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace RedisFPC;
class RedisFPC
{
/**
* php redis的访问类
* @var unknown
*/
private $redis;
/**
* 构造函数
* @param array $redis 使用phpredis的类
* @param 是否连接成功
*/
public function __construct($redis = [])
{
//$this->redis = $redis;
$this->redis = new \Redis();
return $this->redis->connect('127.0.0.1');
}
/**
* 记录对应的缓存,如果之前存在则返回原本的缓存
* @param string $cacheName 缓存名
* @param string | callback $urlOrCallback 需要缓存的数据地址.可以是一个 网页地址也一个可回调类型,如果不是可回调类型,则判定是一个网址
* @param null | int $ttl 缓存过期时间,如果不过期就是用默认值null
* @throws \Exception 如果无法访问地址
* @return boolean|string 缓存成功返回获取到的页面地址
*/
public function remember($cacheName, $urlOrCallback, $ttl = null)
{
$value = $this->get($cacheName);//检查缓存是否存在
if (!$value) {
//之前没有使用键
if (is_callable($urlOrCallback)) {
$text = $urlOrCallback();
} else {
//如果不是回调类型,则尝试读取网址
$text = $this->getUrlText($urlOrCallback);
}
if (empty($text)) {
throw new \Exception('can not get value:' . $urlOrCallback);
}
$this->put($cacheName, $text, $ttl);
return $text;
} else {
return $value;
}
}
/**
* 获取对应的缓存值
* @param string $cacheName 缓存名
* @return String | Bool,如果不存在返回false,否则返回对应的缓存页信息
*/
public function get($cacheName)
{
return $this->redis->get($this->getKey($cacheName));
}
/**
* 将对应的全页缓存保存到对应redis中
* @param string $cacheName 缓存名
* @param string $value
* @param null | int $ttl 过期时间,如果不过期就是用默认值null
* @return boolean 保存成功返回true
*/
public function put($cacheName, $value, $ttl = null)
{
if (is_null($ttl)) {
return $this->redis->set($this->getKey($cacheName), $value);
} else {
return $this->redis->set($this->getKey($cacheName), $value, $ttl);
}
}
/**
* 删除对应缓存
* @param string $cacheName 缓存名
*/
public function delete($cacheName)
{
return $this->redis->delete($this->getKey($cacheName));
}
/**
* 更新缓存,并返回当前的缓存
* @param string $cacheName 缓存名
* @param string | callback $urlOrCallback 需要缓存的数据地址.可以是一个 网页地址也一个可回调类型,如果不是可回调类型,则判定是一个网址
* @param null | int $ttl 过期时间,如果不过期就是用默认值null
* @return boolean|string 缓存成功返回获取到的页面地址
*/
public function refresh($cacheName, $urlOrCallback, $ttl = null)
{
$this->delete($cacheName);
return $this->remember($cacheName, $urlOrCallback, $ttl);
}
/**
* 获取对应的url的信息
* @param string $url 对应的地址
* @return boolean|string
*/
public function getUrlText($url)
{
if (empty($url)) {
return false;
}
return file_get_contents($url);
}
/**
* 生成全页缓存键名
* @param string $cacheName 需要缓存的名称
* @return string 对应的在redis中的键名
*/
private function getKey($cacheName)
{
return 'FPC:'. $cacheName;
}
}