TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Cache or not to cache? (http://www.talkphp.com/absolute-beginners/1665-cache-not-cache.html)

Brook 12-08-2007 04:44 AM

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!)

Jay 12-08-2007 06:00 AM

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()



Brook 12-08-2007 04:19 PM

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?

Karl 12-08-2007 04:40 PM

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.

Jay 12-08-2007 04:41 PM

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();
        }
    }



Brook 12-08-2007 04:53 PM

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


All times are GMT. The time now is 10:01 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0