-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCachePagesZDR.php
131 lines (109 loc) · 4.07 KB
/
CachePagesZDR.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
<?php
/*
* Writen By Zdravko Shishmanov
* email: zdrsoft {@} gmail.com
* Last update: 25.03.2015
*/
// Cache directory - define full path (with slash at the end)
define('CACHE_DIRECTORY', __DIR__ . '/tmp/');
// set in seconds
define('CACHING_TIME_SECONDS', 5);
// set in seconds // Interval for clearing of garbage cache files
define('CLEAR_OLD_CACHE_FILES_TIME_SECONDS', 604800); // One week 604800 seconds
//==============================================================================
class CachePagesZDR
{
protected $cacheTime;
protected $cacheFile;
//==========================================================================
public function pageCacheStart() {
$this->initCacheSettings();
if($this->cacheFileNeedUpdate($this->cacheFile, $this->cacheTime)) {
// start the output buffer
ob_start();
} else {
$this->printCachedPage();
}
}
//==========================================================================
public function pageCacheEnd() {
$pageContent = ob_get_contents();
$this->writeFile($this->cacheFile, $pageContent);
ob_end_flush();
$this->clearOldCacheFiles(CACHE_DIRECTORY, CLEAR_OLD_CACHE_FILES_TIME_SECONDS);
}
//==========================================================================
protected function initCacheSettings() {
if (!is_writable(CACHE_DIRECTORY)) {
echo 'Please make cache directory '.CACHE_DIRECTORY.' writeable.';
return false;
}
$this->cacheFile = $this->initCacheFile();
$this->cacheTime = CACHING_TIME_SECONDS;
return true;
}
//==========================================================================
protected function initCacheFile() {
$tmp = CACHE_DIRECTORY . $_SERVER["REQUEST_URI"];
if(strstr($tmp,'?')) {
$tmp = explode('?', $tmp);
$tmp = $tmp[0];
}
$tmp = explode('/', $tmp);
$cacheFileName = end($tmp);
$tmpString = '';
foreach($_GET as $key => $value) {
$tmpString .= $key.$value;
}
foreach($_POST as $key => $value) {
$tmpString .= $key.substr($value, 0, 10);
}
$cacheFileNameRes = CACHE_DIRECTORY .
$cacheFileName .
'_' .
md5($tmpString) .
'_cache';
return $cacheFileNameRes;
}
//==========================================================================
protected function clearOldCacheFiles($cacheDirectory, $timeInterval) {
$result = false;
$timeClearCacheFile = $cacheDirectory . 'clearCacheFileTimeFile.txt';
if($this->cacheFileNeedUpdate($timeClearCacheFile,
$timeInterval)) {
$dir = dir($cacheDirectory);
while (false !== ($entry = $dir->read())) {
if($entry=='.' || $entry=='..') continue;
$fileNameTmp = $cacheDirectory . $entry;
unlink($fileNameTmp);
}
$dir->close();
$result = $this->writeFile($timeClearCacheFile, '');
}
return $result;
}
//==========================================================================
private function printCachedPage() {
include($this->cacheFile);
exit;
}
//==========================================================================
protected function cacheFileNeedUpdate($filePath, $timeInterval) {
if (!file_exists($filePath)) {
return true;
}
if ((time() - $timeInterval) < filemtime($filePath)) {
return false;
} else {
return true;
}
}
//==========================================================================
protected function writeFile($filePath, $content) {
$fp = fopen($filePath, 'w');
$res = fwrite($fp, $content);
fclose($fp);
return $res;
}
}
?>