c_cache.php
3.47 KB
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
<?php
require_once LIBDIR . 'mutualExclusion.php';
class c_cache
{
private $lock;
private $cacheFile;
private $scriptChanged;
// generiere den Cache Filename aus dem
// 1. script Namen
// 2. einem Hash ueber alle verwendeten:
// - request parameter
// - Werte in GLOBALS
// - Sessionvariablen
// ausserdem wird das cache Verzeichnis auch gleich angelegt wenn es noch
// nicht existiert.
function __construct ($params=NULL, $globals=NULL, $session=NULL)
{
$cacheDir = CACHEDIR . substr ($_SERVER['PHP_SELF'], 1);
$cacheDirArr = explode ('/', $cacheDir);
$cacheParms = array ();
$cacheGlobals = array ();
$cacheSession = array ();
$cachaAllParm = array ();
$parmStr = '';
// cache Verzeichnis anlegen.
$_dir = '';
foreach ($cacheDirArr as $dir)
if (! is_dir ($_dir .= '/' . $dir) && $_dir !== '')
mkdir ($_dir, 0755);
unset ($_dir, $dir, $cacheDirArr);
// alle verwendeten req parameter holen
if ($params !== NULL)
foreach ($params as $parm)
if (isset ($_REQUEST[$parm]))
$cacheParms[$parm] = $_REQUEST[$parm];
else
$cacheParms[$parm] = '';
ksort ($cacheParms);
// alle verwendeten globals holen
if ($globals !== NULL)
foreach ($globals as $glob)
if (isset ($GLOBALS[$glob]))
$cacheGlobals[$glob] = $GLOBALS[$glob];
else
$cacheGlobals[$glob] = '';
ksort ($cacheGlobals);
// alle verwendeten session variablen holen
if ($session !== NULL)
foreach ($session as $sess)
if (isset ($SESSION[$sess]))
$cacheSession[$sess] = $SESSION[$sess];
else
$cacheSession[$sess] = '';
ksort ($cacheSession);
// und nu aus all denen einen md5 hash generieren.
$cacheAllParm = array_merge ($cacheParms, $cacheGlobals, $cacheSession);
unset ($cacheParms, $cacheGlobals, $cacheSession);
foreach ($cacheAllParm as $name => $value)
$parmStr .= '_' . $name . '=' . $value;
$parmStr = md5 ($parmStr);
$this->lock = NULL;
$this->cacheFile = $cacheDir . '/cache_' . $parmStr . '.html';
if (file_exists ($this->cacheFile))
{
if (filemtime ($this->cacheFile) < filemtime ($scriptFile))
$this->scriptChanged = TRUE;
else
{
$incFiles = array_unique (array_merge (
get_included_files (), get_required_files ()));
foreach ($incFiles as $incFile)
{
if (filemtime ($this->cacheFile) < filemtime ($incFile))
{
$this->scriptChanged = TRUE;
break;
}
}
$this->scriptChanged = FALSE;
}
}
}
function check ($interval=0)
{
$scriptFile = HOME . substr ($_SERVER['PHP_SELF'], strlen (URLPATH));
ob_start ();
if ($interval <= 0)
{
$this->lock = NULL;
return FALSE;
}
print "<pre>";
var_dump ($this->scriptChanged);
print "</pre>";
clearstatcache ();
if (! $this->scriptChanged &&
file_exists ($this->cacheFile) &&
time () - filemtime ($this->cacheFile) < $interval)
{
readfile ($this->cacheFile);
return TRUE;
}
else
{
$this->lock = acquireLock ($this->cacheFile, '1');
// inszwischen koennte der cache von einem anderen aufruf
// aktualisiert worden sein, also nochmal checken.
clearstatcache ();
if (file_exists ($this->cacheFile) &&
time () - filemtime ($this->cacheFile) < $interval)
{
releaseLock ($this->lock);
readfile ($this->cacheFile);
return TRUE;
}
return FALSE;
}
}
function update ()
{
$page = ob_get_flush ();
if ($this->lock !== NULL)
{
file_put_contents ($this->cacheFile, $page);
releaseLock ($this->lock);
}
}
};
?>