-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.class.php
291 lines (267 loc) · 6.29 KB
/
cache.class.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Simple Cache class
* API Documentation: https://github.com/cosenary/Simple-PHP-Cache
*
* @author Christian Metz
* @since 22.12.2011
* @copyright Christian Metz - MetzWeb Networks
* @version 1.2
* @license BSD http://www.opensource.org/licenses/bsd-license.php
*/
class Cache {
/**
* The path to the cache file folder
*
* @var string
*/
private $_cachepath = 'cache/';
/**
* The name of the default cache file
*
* @var string
*/
private $_cachename = 'default';
/**
* The cache file extension
*
* @var string
*/
private $_extension = '.cache';
/**
* Default constructor
*
* @param string|array [optional] $config
* @return void
*/
public function __construct($config = null) {
if (true === isset($config)) {
if (is_string($config)) {
$this->setCache($config);
} else if (is_array($config)) {
$this->setCache($config['name']);
$this->setCachePath($config['path']);
$this->setExtension($config['extension']);
}
}
}
/**
* Check whether data accociated with a key
*
* @param string $key
* @return boolean
*/
public function isCached($key) {
if (false != $this->_loadCache()) {
$cachedData = $this->_loadCache();
return isset($cachedData[$key]['data']);
}
}
/**
* Store data in the cache
*
* @param string $key
* @param mixed $data
* @param integer [optional] $expiration
* @return object
*/
public function store($key, $data, $expiration = 0) {
$storeData = array(
'time' => time(),
'expire' => $expiration,
'data' => $data
);
if (true === is_array($this->_loadCache())) {
$dataArray = $this->_loadCache();
$dataArray[$key] = $storeData;
} else {
$dataArray = array($key => $storeData);
}
$cacheData = json_encode($dataArray);
file_put_contents($this->getCacheDir(), $cacheData);
return $this;
}
/**
* Retrieve cached data by its key
*
* @param string $key
* @param boolean [optional] $timestamp
* @return string
*/
public function retrieve($key, $timestamp = false) {
$cachedData = $this->_loadCache();
(false === $timestamp) ? $type = 'data' : $type = 'time';
return $cachedData[$key][$type];
}
/**
* Erase cached entry by its key
*
* @param string $key
* @return object
*/
public function erase($key) {
$cacheData = $this->_loadCache();
if (true === is_array($cacheData)) {
if (true === isset($cacheData[$key])) {
unset($cacheData[$key]);
$cacheData = json_encode($cacheData);
file_put_contents($this->getCacheDir(), $cacheData);
} else {
throw new Exception("Error: erase() - Key '{$key}' not found.");
}
}
return $this;
}
/**
* Erase all expired entries
*
* @return integer
*/
public function eraseExpired() {
$cacheData = $this->_loadCache();
if (true === is_array($cacheData)) {
$counter = 0;
foreach ($cacheData as $key => $entry) {
if (true === $this->_checkExpired($entry['time'], $entry['expire'])) {
unset($cacheData[$key]);
$counter++;
}
}
if ($counter > 0) {
$cacheData = json_encode($cacheData);
file_put_contents($this->getCacheDir(), $cacheData);
}
return $counter;
}
}
/**
* Erase all cached entries
*
* @return object
*/
public function eraseAll() {
$cacheDir = $this->getCacheDir();
if (true === file_exists($cacheDir)) {
$cacheFile = fopen($cacheDir, 'w');
fclose($cacheFile);
}
return $this;
}
/**
* Load appointed cache
*
* @return mixed
*/
private function _loadCache() {
if (true === file_exists($this->getCacheDir())) {
$file = file_get_contents($this->getCacheDir());
return json_decode($file, true);
} else {
return false;
}
}
/**
* Get the cache directory path
*
* @return string
*/
public function getCacheDir() {
if (true === $this->_checkCacheDir()) {
$filename = $this->getCache();
$filename = preg_replace('/[^0-9a-z\.\_\-]/i', '', strtolower($filename));
return $this->getCachePath() . $this->_getHash($filename) . $this->getExtension();
}
}
/**
* Get the filename hash
*
* @return string
*/
private function _getHash($filename) {
return sha1($filename);
}
/**
* Check whether a timestamp is still in the duration
*
* @param integer $timestamp
* @param integer $expiration
* @return boolean
*/
private function _checkExpired($timestamp, $expiration) {
$result = false;
if ($expiration !== 0) {
$timeDiff = time() - $timestamp;
($timeDiff > $expiration) ? $result = true : $result = false;
}
return $result;
}
/**
* Check if a writable cache directory exists and if not create a new one
*
* @return boolean
*/
private function _checkCacheDir() {
if (!is_dir($this->getCachePath()) && !mkdir($this->getCachePath(), 0775, true)) {
throw new Exception('Unable to create cache directory ' . $this->getCachePath());
} elseif (!is_readable($this->getCachePath()) || !is_writable($this->getCachePath())) {
if (!chmod($this->getCachePath(), 0775)) {
throw new Exception($this->getCachePath() . ' must be readable and writeable');
}
}
return true;
}
/**
* Cache path Setter
*
* @param string $path
* @return object
*/
public function setCachePath($path) {
$this->_cachepath = $path;
return $this;
}
/**
* Cache path Getter
*
* @return string
*/
public function getCachePath() {
return $this->_cachepath;
}
/**
* Cache name Setter
*
* @param string $name
* @return object
*/
public function setCache($name) {
$this->_cachename = $name;
return $this;
}
/**
* Cache name Getter
*
* @return void
*/
public function getCache() {
return $this->_cachename;
}
/**
* Cache file extension Setter
*
* @param string $ext
* @return object
*/
public function setExtension($ext) {
$this->_extension = $ext;
return $this;
}
/**
* Cache file extension Getter
*
* @return string
*/
public function getExtension() {
return $this->_extension;
}
}