Article

Home » Server-side Coding » PHP & MySQL Tutorials » Saving Resources with PHPCache

Saving Resources with PHPCache

By Chris Beasley

March 8th, 2002

Reader Rating: 7.5

Page: 1 2 3 4 Next

Databases are spectacular tools -- using one at the back of your Website can give you tremendous power in terms of flexibility and maintenance. A database effectively separates your content from the design, allowing you to edit one without affecting the other. This, coupled with the development of newer scripting languages like PHP and free databases like MySQL, has seen the popularity of database-driven sites increase.

However, even though database-driven Websites offer many benefits over their traditional, static counterparts, there are two major downsides to running a dynamic site. The first is that people tend to use query strings (anything following a "?" in the URL), to pass the correct information to their scripts. This approach can create problems with search engines, however, there are a few solutions to this issue.

The second problem with a dynamic site is that it can create a high server load, and as your site grows in popularity, it may become slower due to an increase in the number of database queries being performed. In this article we’ll look at a solution to this problem – the implementation of a caching system for your site.

There are different ways to decrease the server load your site generates. Some recommend the use of static rather than dynamic pages, and the periodic running of a script or program that generates the new static page. This approach works -- but what if you still want some elements served dynamically on the page in question?

Well, you could write a script that generates another script periodically. For instance, instead of using a PHP script to generate an HTML file, you could use a PHP script to generate another PHP script. In such a case, the generated script would cause less server overhead than its parent. While this approach might succeed, it may also create more work – you’ll need to write a different script to generate each page that you run using this technique. Also, if you choose to generate HTML pages, and then change your mind later, you might have to wait a month or more to have all your new pages indexed by search engines. And in that time, you could easily lose your results rankings.

Instead, why not use a caching system? This tool allows you to cache parts of your script, such as the database queries, while it keeps other elements completely dynamic. So how do you write a caching system? Well, you don’t need to, because Nathan at 0x00.org has written a great system and released it under the General Public License. This article will teach you how to install and use this script on your own site.

Installing phpCache

First, download the script. Unzip it -– the only file you need is phpCache.inc. View the other included files at your leisure: they may give you ideas on different ways to use the script, but for the purposes of this article they aren’t needed.

Once you’ve extracted the file, consider renaming it. It’s a bad idea to put any script that doesn’t end with the proper file extension in any public area on your server. This is because there could be sensitive information in that file, and if a user tried to access it directly, they’d be able to: if the file doesn’t have the correct extension, the code inside it will be displayed in the browser. So it’s a good idea to either rename the file using a .php extension, or store it in a directory above your root public directory.

There are a few things you’ll need to edit inside the file itself. Open the file in your favorite text editor and look for this line:

define(CACHE_DIR, "/tmp/phpCache/");

Unless you have your own server you’ll need to edit that code. You should make a tmp directory above your root html directory, and inside it place a phpCache directory. Then enter the path to your new directory like so:

define(CACHE_DIR, "/home/username/tmp/phpCache/");

If you don’t know the path to your directory, ask your server administrator or log into a shell session and use the pwd command.

The other thing you may need to edit is the key function. phpCache uses a key to store the cached data. The key is generated by default using GET and POST variables as well as your query string. If you’re using another method to pass information to your dynamic pages, then you’ll need to edit this function in order to take those variables into account.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links