-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaching.php
More file actions
44 lines (36 loc) · 1.62 KB
/
caching.php
File metadata and controls
44 lines (36 loc) · 1.62 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
<?php
/**
* Signals via headers that page should not be cached at all
*/
function noCache(){
header('Expires: '.date("D, d M Y H:i:s").' GMT', true);
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0', true);
header('Pragma: no-cache', true);
}
/**
* Signals via headers that the page should be cached for a certain amount of seconds
* @param int $seconds Seconds how long the page should be cached
* @param bool $private If true only the client is allowed to cached, proxies not (default false)
*/
function setCache($seconds=0, $private=false){
if(!$seconds || !is_int($seconds) || $seconds<=0){ noCache(); return true; }
header("Cache-Control: ".($private ? "private" : "public").", max-age=".$seconds, true);
}
/**
* Signals via headers that the page should be cached until a given UTC time in seconds
* @param int $timeUTCSec UTC time in seconds until page should be cached
* @param bool $private If true only the client is allowed to cached, proxies not (default false)
*/
function setCacheUntil($timeUTCSec, $private=false){
setCache($timeUTCSec-time(), $private);
}
/**
* Signals via headers that the page should be cached until the next full given unit (e.g. until the next full hour)
* @param int $unitSeconds Seconds defining the unit interval (e.g. 3600 for hourly)
* @param bool $private If true only the client is allowed to cached, proxies not (default false)
*/
function setCacheUntilFullUnit($unitSeconds=3600, $private=false){
$time = time();
setCache((intval($time / $unitSeconds) + 1)*$unitSeconds - $time, $private);
}
?>