|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class FileCache |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2019, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace icy2003\php\icomponents\cache; |
|
11
|
|
|
|
|
12
|
|
|
use icy2003\php\I; |
|
13
|
|
|
use icy2003\php\icomponents\file\LocalFile; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* 文件缓存 |
|
17
|
|
|
*/ |
|
18
|
|
|
class FileCache extends Base |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* 文件缓存根目录 |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_pathRoot; |
|
27
|
|
|
/** |
|
28
|
|
|
* 文件缓存目录层级 |
|
29
|
|
|
* |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $_directoryLevel = 1; |
|
33
|
|
|
/** |
|
34
|
|
|
* 缓存后缀 |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $_cacheFileSuffix = '.bin'; |
|
39
|
|
|
/** |
|
40
|
|
|
* 目录权限 |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $_dirMode = 0775; |
|
45
|
|
|
/** |
|
46
|
|
|
* 文件权限 |
|
47
|
|
|
* |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $_fileMode; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* 构造函数 |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $pathRoot 缓存根目录 |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct($pathRoot = null) |
|
58
|
|
|
{ |
|
59
|
|
|
if (null === $pathRoot) { |
|
60
|
|
|
$this->_pathRoot = (string)I::getAlias('@icy2003/php_runtime/cache'); |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->_pathRoot = $pathRoot; |
|
63
|
|
|
} |
|
64
|
|
|
$local = new LocalFile(); |
|
65
|
|
|
$local->isFile($this->_pathRoot) || $local->createDir($this->_pathRoot); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* 设置一个缓存 |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $key 缓存键 |
|
72
|
|
|
* @param mixed $value 缓存值 |
|
73
|
|
|
* @param int $duration 有效期,默认 0,表示永久 |
|
74
|
|
|
* |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
public function set($key, $value, $duration = 0) |
|
78
|
|
|
{ |
|
79
|
|
|
$value = serialize($value); |
|
80
|
|
|
$fullKey = $this->buildKey($key); |
|
81
|
|
|
return $this->_setValue($fullKey, $value, $duration); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* 获取一个缓存 |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $key 缓存键 |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
public function get($key) |
|
92
|
|
|
{ |
|
93
|
|
|
$fullKey = $this->buildKey($key); |
|
94
|
|
|
$value = $this->_getValue($fullKey); |
|
95
|
|
|
return false === $value ? false : unserialize($value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* 删除一个缓存 |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $key 缓存键 |
|
102
|
|
|
* |
|
103
|
|
|
* @return boolean |
|
104
|
|
|
*/ |
|
105
|
|
|
public function delete($key) |
|
106
|
|
|
{ |
|
107
|
|
|
$fullKey = $this->buildKey($key); |
|
108
|
|
|
return $this->_deleteValue($fullKey); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* 创建一个 key |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $key |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function buildKey($key) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->_keyPrefix . md5($key); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* 设置一个缓存 |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $fullKey 缓存键全名 |
|
127
|
|
|
* @param mixed $value 缓存值 |
|
128
|
|
|
* @param int $duration 有效期,默认 0,表示永久 |
|
129
|
|
|
* |
|
130
|
|
|
* @return boolean |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function _setValue($fullKey, $value, $duration = 0) |
|
133
|
|
|
{ |
|
134
|
|
|
$cacheFile = $this->_getCacheFile($fullKey); |
|
135
|
|
|
$local = new LocalFile(); |
|
136
|
|
|
$dirname = $local->getDirname($cacheFile); |
|
137
|
|
|
$local->createDir($dirname, $this->_dirMode); |
|
138
|
|
|
if (false !== file_put_contents($cacheFile, $value, LOCK_EX)) { |
|
139
|
|
|
null !== $this->_fileMode && @chmod($cacheFile, $this->_fileMode); |
|
140
|
|
|
0 >= $duration && $duration = 3600 * 24 * 365 * 10; |
|
141
|
|
|
return @touch($cacheFile, $duration + time()); |
|
142
|
|
|
} else { |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* 获取一个缓存 |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $fullKey 缓存键全名 |
|
151
|
|
|
* |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function _getValue($fullKey) |
|
155
|
|
|
{ |
|
156
|
|
|
$cacheFile = $this->_getCacheFile($fullKey); |
|
157
|
|
|
if ((new LocalFile())->isFile($cacheFile)) { |
|
158
|
|
|
if (filemtime($cacheFile) > time()) { |
|
159
|
|
|
return file_get_contents($cacheFile); |
|
160
|
|
|
} |
|
161
|
|
|
$this->_deleteValue($fullKey); |
|
162
|
|
|
} |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* 删除一个缓存 |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $fullKey 缓存键全名 |
|
170
|
|
|
* |
|
171
|
|
|
* @return boolean |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function _deleteValue($fullKey) |
|
174
|
|
|
{ |
|
175
|
|
|
$cacheFile = $this->_getCacheFile($fullKey); |
|
176
|
|
|
return (new LocalFile())->deleteFile($cacheFile); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* 获取缓存文件 |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $fullKey 缓存键全名 |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function _getCacheFile($fullKey) |
|
187
|
|
|
{ |
|
188
|
|
|
if ($this->_directoryLevel > 0) { |
|
189
|
|
|
$base = $this->_pathRoot; |
|
190
|
|
|
for ($i = 0; $i < $this->_directoryLevel; ++$i) { |
|
191
|
|
|
if (false !== ($prefix = substr($fullKey, $i + $i, 2))) { |
|
192
|
|
|
$base .= DIRECTORY_SEPARATOR . $prefix; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $base . DIRECTORY_SEPARATOR . $fullKey . $this->_cacheFileSuffix; |
|
197
|
|
|
} else { |
|
198
|
|
|
return $this->_pathRoot . DIRECTORY_SEPARATOR . $fullKey . $this->_cacheFileSuffix; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* 清空缓存 |
|
204
|
|
|
* |
|
205
|
|
|
* @return boolean |
|
206
|
|
|
*/ |
|
207
|
|
|
public function clear() |
|
208
|
|
|
{ |
|
209
|
|
|
return (new LocalFile())->deleteDir($this->_pathRoot, false); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|