keybase
[m6w6/m6w6.github.io] / _posts / 2004-08-04-dropping-server-load-with-http-caching.md
1 ---
2 title: Dropping server load with HTTP caching
3 author: m6w6
4 tags:
5 - PHP
6 ---
7
8 Ever watched youself browsing e.g. a web forum?
9 Noticed that you viewed the same page several times?
10
11 Well, this means extraordinary **and** useless load for your server if there's
12 no caching mechanism implemented in the web application. Even if there is some
13 file or db cache you can still improve performance with implementing some http
14 cache.
15
16 The easiest way is using PEARs
17 [HTTP::Header](http://pear.php.net/package/HTTP_Header):
18
19 ```php
20 require_once 'HTTP/Header/Cache.php';
21 // only cache a few seconds for frequently
22 // changing pages like a forum;
23 // Header_Cache will exit automatically with
24 // "HTTP 304 Not Modified" if the page is
25 // requested twice within 3 seconds
26 $cache = &new HTTP_Header_Cache(3, 'seconds');
27 $cache->sendHeaders();
28
29 // ... load from file/db cache
30 // ... or rebuild page
31 ```
32
33 ~~Probably the greatest caveat of using HTTP caching is, that our _beloved_
34 Internet Explorer with standard settings doesn't even send an request to the
35 server if it just **notices** a "Last-Modified" header (sic!), so one'd have
36 to press the reload button (without CTRL) that a new page is displayed after
37 the defined amount of time.~~
38
39 ~~A dead end again? Decide yourself...~~
40
41 I finally managed to implement support for Internet Explorer in HTTP_Header
42 1.1.0 :D