Caching for dynamic content
I made this snippet to show how to use the Last-Modified and the ETag header
<br/>
to optimize the caching of a website. If used correctly this will speed up
<br/>
your page loads.
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
// Start output buffering, this will
// catch all content so that we can
// do some calculations
ob_start();
// Some example HTML
print '<html>';
// put your content in here:
print '<h1>Example content</h1>';
print '<ul>';
for ($x=0; $x < 10; $x++)
print "<li>List item $x</li>";
print '</ul>';
print '</html>';
// or include() something here
// Now save all the content from above into
// a variable
$PageContent = ob_get_contents();
// And clear the buffer, so the
// contents will not be submitted to
// the client (we do that later manually)
ob_end_clean();
// Generate unique Hash-ID by using MD5
$HashID = md5($PageContent);
// Specify the time when the page has
// been changed. For example this date
// can come from the database or any
// file. Here we define a fixed date value:
$LastChangeTime = 1144055759;
// Define the proxy or cache expire time
$ExpireTime = 3600; // seconds (= one hour)
// Get request headers:
$headers = apache_request_headers();
// you could also use getallheaders() or $_SERVER
// or HTTP_SERVER_VARS
X
Url: http://www.jonasjohn.de/snippets/php/caching.htm
Language: PHP | User: ShareMySnippets | Created: Oct 16, 2013 | Tags: caching