note 34242 deleted from ref.mysql by didou
| From: | didou@php.net | Date: | Wed, 01 Oct 2003 12:09:30 +0000 |
| Subject: | note 34242 deleted from ref.mysql by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57529@lists.php.net to get a copy of this message | ||
Note Submitter: maarten.malaise@pandora.be
----
The amount of database-request was getting to high on my server, so I made a small cache/caching
solution in PH, allowing me to increase the speed of page-deliveries to my visitors. It worked
enormously well. If you're experiencing load-problems, you might as well try this solution in
stead of buying a second webserver.
In the example below, I get a news-article from the database. Consider this code:
page is called like this: http://www.YOURDOMAIN.com/article.php?id=123
<?
############################################
#
# change the requested url into a
# correct filename (based on the called URI)
# for storing in the cache directory
#
############################################
$filemod=ereg_replace("\/","_",$HTTP_SERVER_VARS[REQUEST_URI]);
$filemod=ereg_replace("\?","_",$filemod);
$filemod=ereg_replace("\&","_",$filemod);
$filemod=ereg_replace("\=","_",$filemod);
$filemod=ereg_replace("\.","_",$filemod);
$filename = "cache/".$filemod.".htm";
############################################
#
# read the cache-dir and see if the page was
# already cached. If it was cached before,
# read the cache and quit the script
#
############################################
if (file_exists($filename)){
readfile($filename);
#echo "cache";
die();
}
############################################
#
# start the buffer
#
############################################
ob_start();
############################################
#
# query database and create HTML-output
#
############################################
$link = mysql_connect('HOST', 'UID', 'PASS') or die ("Could not
connect");
mysql_select_db('DATABASE_NAME') or die ("Could not select database");
$query = "SELECT title,article FROM news WHERE id=".$HTTP_GET_VARS['id'];
$result = mysql_query($query) or die("Query failed");
while ($line = mysql_fetch_array($result)) {
$title=$line['title'];
$article=$line['article'];
}
mysql_free_result($result);
mysql_close($link);
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><? echo $title; ?> - SITENAME</title>
</head>
<body>
<h1><? echo $title; ?></h1>
<p><? echo $article; ?></p>
</body>
</html><?
##########################################
#
# no data is written to the output yet.
# First we read the contents of the buffer
# and then we flush the data to the client
#
##########################################
$page=ob_get_contents();
ob_end_flush();
##########################################
#
# and here we write the page to the
# cache-directory as well
#
##########################################
$fd = fopen ($filename, "w");
fwrite ($fd, $page);
fclose($fd);
?>
What happens? You start the script
If the cache page exists
1. read and print the cached page
2. quit the script
If the cached page doesn't exis yet:
1. start a buffer,
2. read the database-content
3. create the html
4. read the buffer as a string
5. flush the buffer
6. write the string as a file to the cache-directory
Remember to set the right permissions to the cache dir (CHMOD).
You can use this caching-solution in combination with a content-management-system. If you make a
change to an article, you can automate an unlink (i.e. 'delete') of that specific file in
the cache-dir. If you change the lay-out of your site, then you only have to empty the whole
cache-directory to apply the changes and recreate all pages.
Disadvantage of this solution is that you need some webspace for storing the cache. If you have
thousands of webpages (i.e. news-articles in the example above), then you will have thousands of
webpages in the cache-directory.
Good luck !
Maarten Malaise
http://www.malaise.info/maarten
PS: remember me and send me a dollar if this turns out to be a solution for your problem ;-)
PS2: I need a job, my current employer is running out of money