-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.php
More file actions
37 lines (37 loc) · 1.12 KB
/
cache.php
File metadata and controls
37 lines (37 loc) · 1.12 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
<?php
// Number of seconds a page should remain cached for
$cache_expires = 33600;
// Path to the cache folder
$cache_folder = "cache/";
// Checks whether the page has been cached or not
function is_cached($file) {
global $cache_folder, $cache_expires;
$cachefile = $cache_folder . $file;
$cachefile_created = (file_exists($cachefile)) ? @filemtime($cachefile) : 0;
return ((time() - $cache_expires) < $cachefile_created);
}
// Reads from a cached file
function read_cache($file) {
global $cache_folder;
$cachefile = $cache_folder . $file;
return file_get_contents($cachefile);
}
// Writes to a cached file
function write_cache($file, $out) {
global $cache_folder;
$cachefile = $cache_folder . $file;
$fp = fopen($cachefile, 'w');
fwrite($fp, $out);
fclose($fp);
}
// Let's begin, first work out the cached filename
$cache_file = md5($_SERVER['REQUEST_URI']) . ".html";
// Check if it has already been cached and not expired
// If true then we output the cached file contents and finish
if (is_cached($cache_file)) {
echo read_cache($cache_file);
exit();
}
// Ok so the page needs to be cached
// Turn on output buffering
ob_start();