Gameon's Favourite Blogs >>
Anoop : : GameOn BuZZ ||                     || Hari S R : : Tranquility ||                    || ~==[[[ Abhi ]]]==~ : : Cogito Ergo Sum ||                    || Abhilash Sir : : A Leaf out of Life ||

Programming Simplified

A Freelancers Tips and Tricks On Programming With Php , Ajax , CGI -PEARL ,Javascripting , CSS and GM(grease-monkey)..




Speed up your scripts by caching mysql data

As you know, the MySQL Server experiencs lots of strain. And When you have a large database, some queries end up taking too long and visitors get the Proxy Error message. Here is a method to modify your site to cache all dynamic data for 1 minute so as to reduce load. By this method your homepage grabs canned data from the server, so that visitors will see something at least. It then waits a while and tries to get an updated copy through AJAX.

(Note that it's a bad idea to cache stuff if your script runs on interaction based on realtime data. Don't do this to your forum, for example. A CMS might be able to benefit somewhat from this. If your site uses ADOdb, it's very simple to tell ADOdb to cache your queries.)

Depending on the nature of your site, you may be able to do the same. Here are some basic PHP functions to help you get started:

// $filename: name of cache file
// $cachetile: how long the cache lasts in seconds
// $data: string data to be cached

function cachefresh($filename,$cachetime) {
if(!file_exists($filename)) $mtime = 0;
else $mtime = filemtime($filename);
$mtime += $cachetime;
if($mtime > time()) return(true);
return(false);
}

function cachedata($filename,$data) {
$fp = fopen($filename,"w+");
fwrite($fp,$data);
fclose($fp);
}

function cachedatacheck($filename,$cachetime,$data) {
if(!cachefresh($filename,$cachetime)) cachedata($filename,$data);
}

cachefresh() returns true if the cached data is still within the time limit. cachedata() puts string data into a file, overwriting original contents. cachedatacheck() puts string data into a file if the cache has expired, overwriting original contents.

One way to use this is:
1) check if cache is fresh. if so, just output the data using readfile() or similar.
2) if not, generate and cache new data before sending it to the browser.



         

           My Photo

           Gameon


Web This Blog


AddThis Social Bookmark Button

Page copy protected against web site content infringement by Copyscape


XML


Powered by Blogger






© 2007 Programming Simplified | ..:nEo:..
No part of the content or the blog may be reproduced without prior written permission.