TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-08-2007, 04:44 AM   #1 (permalink)
The Acquainted
Inquisitive 
 
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
Brook is on a distinguished road
Default Cache or not to cache?

Hi All - I'm just getting into php, and was hoping if you could help me with a few things :)

My main question right now is, should I use some kind of page caching to save the database getting hit so much?

All the scripts I am going to write are probably ok to be updated every 10 minutes, so I was thinking of outputting the final html and caching that and then displaying the cached data/html to the end user. Then perhaps set-up a cron to update the cache every 10 minutes.

Is that a good idea? I figured that rather than the DB get XX amount of queries every second/page view, I thought once every 10 minutes would be easier on the server?

Do you think I am worrying too much?

The pages are basically going to call data from vBulletin and there will be about 22 to 26 queries in total per page.

Your help and thoughts apreciated!

(ps I like the wider layout here!)
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
Brook is offline  
Reply With Quote
Old 12-08-2007, 06:00 AM   #2 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Yea, caching that page sounds like a great idea, depending on how you do it, of course.

The cron idea is great in most circumstances, but in low-traffic sites, the cron could produce more hits than.. visitors.

One thing to think about when timing the cron is: "Will there be a visitor at least every [time * 2] minutes?"

The other alternative to crons would be to make a self check:

(pseudo code)
PHP Code:
if( last_updated >= 10 minutes ago )
{
    
update_cache()

Jay is offline  
Reply With Quote
Old 12-08-2007, 04:19 PM   #3 (permalink)
The Acquainted
Inquisitive 
 
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
Brook is on a distinguished road
Default

Hi Jay, thanks for the info.

There could be up to 40 pages I might use the cahing on - is that too many?

I'm guessing the site will be quite busy... but maybe I should just build the pages for now and see how that goes first?

Do you know f any good tutorials that show you how to cache pages effectively?
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
Brook is offline  
Reply With Quote
Old 12-08-2007, 04:40 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

The Zend Framework includes a very flexible caching class, and as usual, there is ample documentation explaining it's usage. You can view the documentation for the Zend_Cache here.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-08-2007, 04:41 PM   #5 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

No sorry I don't, I have never read a tutorial about caching

What you could do is use MD5() on the query text (ie: "SELECT * FROM"), store that as a filename in a directory like ./cache/sql/<md5 result>.txt, then check the filetime to see how old the cache is

PHP Code:

function mysql_cache_query$query )
{
    
$path './cache/sql/'md5$query ) .'.txt';

    if( 
file_exists$path ) && filemtime$path ) + (60 10) <= time() )
    {
        return 
unserializefile_get_contents$path ) );
    }
    else
    {
        if( 
$result mysql_query$query ) )
        {
            
$file fopen$path'w+' );
            
fwrite$fileserialize$result ) );
            
fclose$file );

            return 
$result;
        }
        else
        {
            return 
mysql_error();
        }
    }

Jay is offline  
Reply With Quote
Old 12-08-2007, 04:53 PM   #6 (permalink)
The Acquainted
Inquisitive 
 
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
Brook is on a distinguished road
Default

Thanks again Jay, and thanks for the info Karl.

It seems like it could be quite involved (caching each query), whereas I was thinking you could just put the entire page content (ie the final HTML) into a variable and then simply cache that.

I am only a beginner so I guess I should build the pages first and see where to go from there. I've subscribed to this thread so will come back to it when I am at that stage.

Thanks again for the quick responses, I really like how this place is shaping up :D
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
Brook is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:37 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design