-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathincl-cache.php
357 lines (304 loc) · 9.47 KB
/
incl-cache.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?/*
SIMPLE PHP HANDLER FOR FILESYSTEM AND DATABASE CACHING
- simpleCache Simple filesystem-based cache for complete pages
- simpleMemcache Simple memcached system
Has routines (start() and update()) for caching pages
and routines (get() and set()) for caching objects
* NOTE that this presumes you already have the PHP Memcache
library installed and configured, and have access to a
working memcached server
- dbCache Simple database cache for complete pages
* NOTE that this presumes you already have a working MySQL
server, configured in incl-dbconnect.php
- objectCache Filesystem-based cache for php objects such;
this is used for caching queries in the required
dbCache database connection object
- fragmentCache Filesystem-based cache for page fragments,
identified by unique programatically-generated keys
* NOTE that this should be deprecated; objectCache
delivers the same results with a cleaner interface
Brad Heath; [email protected]
*/
class simpleMemcache {
private $_servers = array();
private $_serversCount = 0;
private $_ports = array();
private $_m = array();
private $_memcache;
private $_TTL;
private $_defaultTTL = 300;
private $_lastValue;
private $_objectName;
private $_objectIdentifier;
private $_key;
function __construct() {
if(func_num_args() == 1) {
$this->_TTL = func_get_arg(0);
} else {
$this->_TTL = $this->_defaultTTL;
}
$this->_memcache = new Memcache();
}
function start() {
$key = ($_SERVER['REQUEST_URI']);
$this->_key = $key;
if($this->cacheRead('', $key, $ttl)) {
echo $this->_lastValue;
echo "<!--- cached --->";
exit;
} else {
ob_start();
return false;
}
}
function cacheRead($on, $oi, $ttl) {
$this->_objectName = $on;
$this->_objectIdentifier = $oi;
$this->_key = md5($on . $oi);
if($this->_memcache->get($this->_key)) {
$this->_lastValue = unserialize($this->_memcache->get($this->_key));
return $this->_lastValue;
} else {
$this->_lastValue = false;
return false;
}
}
function cachedValue() {
return $this->_lastValue;
}
function cacheWrite($v) {
$this->_memcache->set($this->_key, serialize($v), false, $this->_TTL);
}
function update() {
$this->cacheWrite(ob_get_contents());
}
function server($server, $port) {
$this->_serverCount++;
$this->_servers[$this->_serverCount] = $server;
$this->_ports[$this->_serverCount] = $port;
$this->_memcache->addServer($server, $port);
}
function status() {
return $this->_memcache->getExtendedStats();
}
}
class simpleCache {
private $_cachePath = "/home1/bradheat/cache/"; // relative path to cache files
private $_defaultUpdateTime; // default time of 24 hours
private $_cacheFileName;
private $_updateTime;
private $_enabled = true;
function __construct() {
ob_start();
$this->_defaultUpdateTime = 60*60*24;
$this->_cacheFileName = $this->_cachePath . $this->cache_fn($_SERVER['REQUEST_URI']) . ".cache";
if(func_num_args() == 1) {
$this->_updateTime = func_get_arg(0);
} else {
$this->_updateTime = $this->_defaultUpdateTime;
}
$this->dump();
}
function disable() {
$this->_enabled = false;
}
private function cache_fn($fn) {
// generate unique filename based on passed paramaters
return md5(trim(strtoupper($fn)));
}
function update() {
// update the cache file
if($this->_enabled) {
$bc = ob_get_contents();
$f = fopen( $this->_cacheFileName , "w");
fwrite($f, $bc);
fclose($f);
ob_end_clean;
}
}
function dump() {
// check for existing cache file and display if available and timely
if($this->_enabled) {
if(file_exists($this->_cacheFileName)
&& (filemtime($this->_cacheFileName) > (time() - $this->_updateTime ))) {
include($this->_cacheFileName);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($this->_cacheFileName))." -->";
exit();
}
}
}
}
// dbCache
// cache pages or fragments to a predefined database table (cache.cache);
// requires dbLink database connection class
class dbCache {
private $_cacheTable = 'cache.cache';
private $_defaultUpdateTime;
private $_cacheIdentifier;
private $_updateTime;
private $_db;
private $_makeNewDB;
private $_objectID;
private $_objectIDEncoded;
private $_header;
private $_purgeTime;
function __construct() {
ob_start();
$this->_defaultUpdateTime = 60 * 60;
$this->_db = new dbLink();
$this->checkDB();
if(func_num_args() == 1) {
$this->_updateTime = func_get_arg(0);
} else {
$this->_updateTime = $this->_defaultUpdateTime;
}
$this->_objectID = $this->cache_fn($_SERVER['REQUEST_URI']);
$this->_objectIDEncoded = addslashes($this->_objectID);
$this->dump();
}
function setContentHeader($header) {
$this->_header = $header;
}
function update() {
$this->checkDB();
$bc = ob_get_contents();
$ob = $this->_objectIDEncoded;
$sql = "REPLACE DELAYED INTO " . $this->_cacheTable . " (objectid, stream, header) VALUES(\"$ob\", COMPRESS(\"$bc\"), \"" . $this->_header . "\") ";
$ins = $this->_db->actionQuery($sql);
}
function dump() {
$sql = "SELECT UNCOMPRESS(stream) AS u, header AS hdr FROM cache.cache
WHERE objectid = '" . $this->_objectIDEncoded . "'
AND TIME_TO_SEC(TIMEDIFF(NOW(), created)) <= " . $this->_updateTime . "
";
$h = $this->_db->getHash($sql);
if($h) {
if($h[hdr] != '') {
header($h[hdr]); }
print $h[u];
exit();
}
}
private function cache_fn($fn) {
return md5(trim(strtoupper($fn)));
}
function checkDB() {
$r = $this->_db->query("SELECT NULL FROM cache.cache LIMIT 1");
if(!$r) {
$this->_makeNewDB = true; }
if($this->_makeNewDB == true) {
$sql = "CREATE DATABASE IF NOT EXISTS cache ";
$this->_db->actionQuery($sql);
$sql = "CREATE TABLE IF NOT EXISTS cache.cache(objectid VARCHAR(255) PRIMARY KEY, created TIMESTAMP, stream LONGBLOB, header VARCHAR(255), url VARCHAR(255), KEY url_idx(url), KEY objectid_idx(objectid), KEY created_idx(created)) TYPE=MyISAM ";
$this->_db->actionQuery($sql) or die($this->_db->error());
}
}
function purge() {
$this->checkDB();
if(func_num_args() == 1) {
$this->_purgeTime = func_get_arg(0);
} else {
$this->_purgeTime = $this->_defaultUpdateTime;
}
$sql = "DELETE FROM cache.cache WHERE
TIME_TO_SEC(TIMEDIFF(NOW(), created)) <= " . $this->_updateTime . "
";
$r = $this->_db->actionQuery($sql) or die($sql);
}
}
// cache data objects
class objectCache {
private $_cacheFileName;
private $_updateTime;
private $_objectName;
private $_objectIdentifier;
private $_object;
function __construct() {
}
function cacheFileName ($objectName, $objectIdentifier) {
$this->_cacheFileName = '/cache/' . md5($objectName . '-' . $objectIdentifier) . '.objectcache';
}
function cacheRead($objectName, $objectIdentifier, $updateTime) {
$this->_objectName = $objectName;
$this->_objectIdentifier = $objectIdentifier;
$this->_updateTime = $updateTime;
$this->cacheFileName($objectName, $objectIdentifier);
return $this->dump();
}
function cacheWrite($object) {
$timeCache = microtime(true);
$bc = serialize($object);
$f = fopen( $this->_cacheFileName , "w");
fwrite($f,$bc);
fclose($f);
}
function dump() {
$out = false;
if((file_exists($this->_cacheFileName))
&& (filemtime($this->_cacheFileName) > (time() - $this->_updateTime ))) {
$object = unserialize(file_get_contents($this->_cacheFileName));
$this->_object = $object;
return $object;
} else {
return false;
}
}
function cachedValue() {
return $this->_object;
}
}
// cache mechanism for PORTIONS of pages;
class fragmentCache {
private $_cacheFileName;
private $_updateTime;
private $_fragmentName;
private $_fragmentSpecialName;
function __construct() {
$this->_fragmentName = 0;
}
function startCache() {
// start a new instance of the partial cache
// USAGE IS startCache([timeout in milliseconds], [special cache name])
ob_start();
$this->_fragmentName++;
if(func_num_args() >= 2) {
$this->_fragmentSpecialName = func_get_arg(1);
} else {
$this->_fragmentName++;
$this->_fragmentSpecialName = $this->_fragmentName;
}
$this->_cacheFileName = "c:\\cache\\" . cache_fn($_SERVER['REQUEST_URI']) . "_" . $this->_fragmentSpecialName . ".fragmentcache";
$fn = "cache/" . $this->_cacheFileName . "_" . $this->_fragmentName . ".fragmentcache";
if(func_num_args() >= 1) {
$this->_updateTime = func_get_arg(0);
} else {
$this->_updateTime = 60 * 60 * 24; // one day (60 sec * 60 min * 24 hr)
}
return $this->dump();
}
function endCache() {
$timeCache = microtime(true);
$bc = ob_get_contents();
$f = fopen( $this->_cacheFileName , "w");
fwrite($f, $timeCache . "|" . $bc);
fclose($f);
ob_end_clean;
}
function dump() {
// dump the output of a partial cache
$out = false;
if(file_exists($this->_cacheFileName)) {
$contents = file_get_contents($this->_cacheFileName);
$segs = explode("|",$contents);
$timeNow = microtime(true);
if(($timeNow - $segs[0]) <= $this->_updateTime) {
for($i = 1; $i <= count($segs); $i++) {
print $segs[$i]; }
$out = true;
// exit();
}
}
return $out;
}
}
?>