/    Sign up×
Community /Pin to ProfileBookmark

when should I cache my pages

I have a small site [url]http://rroma.3x.ro/site/index-en.php[/url]

Everything but the text is generated by one small (150 lines) php file.

There isn’t much delay comapred to a static site in this case, but I was wondering, how will I know when it is necessary to cache the output to speed up page loading?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 03.2005 — pages should be cached when the browser colud refer to a previous version instead of requesting the page every time, with PHP this is not recomended as the page usualy should be generated each time(unless your using PHP to include files that are not going to change e.g. a banner's location from a text file).
Copy linkTweet thisAlerts:
@ShrineDesignsFeb 03.2005 — i know with mozilla if the page is not out-dated or an updated version is not found it uses the cached one
Copy linkTweet thisAlerts:
@amazing_andr3authorFeb 03.2005 — Let me make mysqlf clear(er). what I'm talking about is using PHP to catch the output (a page) and save it in a file somewhere. Then when someone requests the same page, rather than run through the script again (which might need to do a lot of complicated stuff like tens of queries to the database), simply get the text from the 'cached' file. Then it should update this file if it is, say, older than one day to reflect changes I might have made.

For this to be worth the effort, one must clearly have a complicated script that would take a noticeable amount of time to execute. The problem is, I can't really know if the page is loading slow because of the time the server takes to execute the script or for another reason.
Copy linkTweet thisAlerts:
@amazing_andr3authorFeb 03.2005 — OK let me say what I have in mind. (didn't wanna say this before in case it turns out to be an awful idea and I make a fool of myself). a script that would generate a 200x200px pie chart (that's 40000 divs!!!). Makes each pixel in its own division and gives it the right color. This crashes the server on my PC when the image gets beyond 50x50.

But if I run this only once a day and store the result, perhaps it might work?
Copy linkTweet thisAlerts:
@ShrineDesignsFeb 04.2005 — [code=php]<?php
function cache_start($days_valid = 1)
{
$file = 'cache/' . md5($_SERVER['REQUEST_URI']);
list($y, $m, $d) = explode('-', date('Y-m-d'));

if(file_exists($file) && date('U', filemtime($file)) >= date('U', mktime(0, 0, 0, $m, $d - $days_valid, $y)))
{
$fp = fopen($file, 'rb');
$buffer = fread($fp, filesize($file));
fclose($fp);
exit($buffer);
}
else
{
ob_start('cache_end');
}
}
function cache_end($buffer)
{
$file = 'cache/' . md5($_SERVER['REQUEST_URI']);
$fp = fopen($file, 'wb');
fwrite($fp, $buffer);
fclose($fp);
return $buffer;
}
?>[/code]
call cache_start() at the beginning of your scripts, change 'cache/' to the directory you wish to use for caching

call ob_end_flush() at the end of your scripts
Copy linkTweet thisAlerts:
@amazing_andr3authorFeb 04.2005 — thanks. it seems to be easy enough. it's just that I am still a bit unsure about when it should be done.
Copy linkTweet thisAlerts:
@ShrineDesignsFeb 04.2005 — if you are using databases, you could check the it the last update was recent, when caching an entire page that is dynamic, the last modified time for the file wouldn't work, you might be able to get the last modified date from the headers, which would be more accuate

as for the duration to server a cached file, if you page doesn't get updated often, like a week, i would server the cached document for 3 or so days before re-caching it
×

Success!

Help @amazing_andr3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...