Most web servers are able to handle “normal” traffic and there are plenty of websites which doesn’t have so much traffic. So maybe you ask yourself: Why should you cache your PHP powered website? The apache web server is able to serve many, many files at the same time, but all these files need to be static. A PHP script is parsed by the web server and next the generated HTML data is send to the client (web browser). While this happens the server need to use much more memory than by sending a file to a web client. Imagine what happens if you run/parse a page which is build with WordPress…
The web has not only human visitors!
If your WordPress or PHP site has a few visitors within an hour a web server should be able to serve ALL pages to your visitors without any problems. So far so good, but what if your site get accessed by some bot? The most bad scenario is that these “unnatural” access can slow/take down your site and also all other sites hosted on the same server!
WordPress Super Cache, a required WP plugin
Even if your blog doesn’t have a lot of blog post or comments, you should install the WordPress Super Cacheplugin. This plugin works on most servers and can rescue your blog’s life! WordPress needs a lot of database queries to show a single page to your visitors. Each database connection needs some memory and will use some CPU. Using this cache plugin a “normal” viewed page doesn’t use the database anymore and your server can handle much more traffic.
Cache functions for custom PHP websites
There are many ways to cache your website, there are several cache modules available or it’s possible to create a cache version of each page using some PHP code. Which the best is for your situation depends on the application and the type of hosting you’re using.
The eAccelerator project
If you’re able to configure your web server (you need root access) you should try the eAccelerator project. It works as kind of PHP extension and is able to create a cache version of your PHP scripts. I installed and updated eAccelerator on two web servers now and I like it how it works. Before you start you should check the requirements and maybe you like to read my notes about the eAccelerator installation.
Custom PHP file caching (tutorial)
If you’re looking for a way to cache single pages from your website you should try this tutorial. The simple code snippet is able to “download” the HTML code from the selected page and stores the HTML code as a static page. The following code will check, read/write and output the cache version (check the comments inside the code).
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
// a function to receive an write some data into a file
function get_and_write($url, $cache_file) {
$string = file_get_contents($url);
$f = fopen($cache_file, 'w');
fwrite ($f, $string, strlen($string));
fclose($f);
return $string;
}
// a function that opens and and puts the data into a single var
function read_content($path) {
$f = fopen($path, 'r');
$buffer = '';
while(!feof($f)) {
$buffer .= fread($f, 2048);
}
fclose($f);
return $buffer;
}
$cache_file = '/home/user/public_html/cache/cache.page.php';
$url = 'http://www.domain.com/page.php';
if (file_exists($cache_file)) { // is there a cache file?
$timedif = (time() - filemtime($cache_file)); // how old is the file?
if ($timedif < 3600*24) { // get a new file 24 hours
$html = read_content($cache_file); // read the content from cache
} else { // create a new cache file
$html = get_and_write($url, $cache_file);
}
} else { // no file? create a cache file
$html = get_and_write($url, $cache_file);
}
echo $html;
exit;
?>
|
The code above is pretty simple but not dynamic. To get this example working, you need to create a list for the cache files and for the URL’s. In the next example we use some mod_rewrite rules to match our file structure.
In our case we use a part from the URL and pass this string as a variable using the query-string. Here are some example URLs:
http://domain.com/page/great-post-about-scripts
http://domain.com/page/php-upload-tutorial
http://domain.com/page/jquery-plugin-review
Inside our .htaccess file we use this rule:
RewriteEngine on
RewriteRule ^page\/([a-z\-]*)$ /page.php?pageurl=$1 [L]
The rewrite engine will pass anything after “page/” to the page.php file as a query-string. Inside the file page.php we need some code to automate our request and generate the cache version.
1
2
3
4
5
6
7
8
|
if (!empty($_GET['pageurl']) && preg_match('/^[a-z\-]*$/', $_GET['pageurl'])) {
$cache_file = '/home/user/public_html/cache/cache-'.$_GET['pageurl'].'.php';
$url = 'http://www.domain.com/page.php?page='.$_GET['pageurl'];
} else {
header ("HTTP/1.1 301 Moved Permanently");
header("Location: ".$url);
exit;
}
|
Place this IF/ELSE statement below the variables $cache_file and $url. The code we’ve used inside the tutorial is just an example, you need to change the code to get it working in your situation.
Note: Only a member of this blog may post a comment.