Speed up page loads using Cache PHP
At work we pull ranks for how key-phrases ranked on certain search engines once a week. We have many clients and each client has multiple key-phrases and each key-phrase has ranks for multiple search engines. This information is stored in the database. This information adds up more and more over time. So the longer a client is with us the more data that is stored in the database. All this information can become cumbersome when trying to calculate and display each time a web page is loaded. This is taxing on our web server to calculate this information and is also taxing on our database when all this information is pulled. The solution to this problem is caching. There are two types of caching we use at work. Memcache and File caching.
Memcache is where the cache is stored on the web servers RAM. This can be good because of the speed of retrieval of the cache. The downfalls of using memcache is that if you restart the web server the information stored in RAM is lost. Also the amount of information you can cache is limited to the size or your RAM on your web server. The RAM limitation can be overcome by having a dedicated memcache server.
File Caching is where the information is stored in a file on the web server. This will be slower than memcache because the information is pulled from the hard drive of the web server instead of being pulled from the web servers RAM. When the web server is restarted the cached information is not lost because it is stored on the hard drive. The limitation of the size of all the caching is limited to the size of the hard drive and not the size of RAM on the web server. Now with solid state hard drives coming out and getting cheaper all the time you can speed up the loading of file caches probably close to the speed of memcache.
My first example will be memcaching. In order to do memcaching on your web server you will have to make sure you have memcache installed. I am running Ubuntu with LAMP, so the command line call to install memcache is ‘sudo apt-get install memcache’. You can download my example file memcache_example.php.tar so that you can follow along easier.
I will not put all the code together for the memcaching example first and then break it down:
* @date 2011-09-30
* @brief example of memcaching
*/
echo "
<html>
<head>
<title>MemCache Example</title>
</head>
<body>
<h1>MemCache Example</h1>
";
if( isset($_GET['client_id']) && is_numeric($_GET['client_id'])) {
$client_id = $_GET['client_id'];
} else {
$client_id = rand(1,20);
}
define('RANK_CACHE_DATE_STRING', 'next Saturday');
$memcache_obj = memcache_connect('localhost');
$cache_till = strtotime(RANK_CACHE_DATE_STRING);
$cache_name = 'rank_table_html_'.$client_id;
if( ! $html = memcache_get($memcache_obj, $cache_name)) {
$html = get_rank_table_html($client_id);
memcache_set($memcache_obj, $cache_name, $html, 0, $cache_till);
echo "<p>";
echo "There was NOT a cache of the HTML so new
";
echo "HTML was created and cached for next time
";
echo "this client is loaded.";
echo "</p>";
} else {
echo "<p>There was a cache of the HTML</p>";
}
echo $html;
function get_rank_table_html($client_id) {
return "
<p>
Here is where the table of ranks
would be for client# $client_id
</p>