Re: Caching query results in html pages

From: Date: Tue, 13 Mar 2007 22:24:40 +0000
Subject: Re: Caching query results in html pages
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-43274@lists.php.net to get a copy of this message
This may work, although I just made it up. I can see already that you'd have some problems with multiple scripts running at once. If a script opens the cache, then a second script saves new cache information before the first script saves it's data, the first script would overwrite the second script's update. Maybe instead of a read at the beginning of script execution, a read at the beginning of each query would be better? Save cache results to a file: <? // script start $querycache = unserialize(file_get_contents("query.cache")); /* $querycache format: array('sql' => array(), 'result'=>array(), 'time'=>array()); For each sql statement 'sql' you have a result array. */ // run all your queries through a function: function query($sql) { $cached = array_search($sql, $querycache['sql']) // check to see if the result is old if (time() - $querycache['time'][$cached] > $max_time) {
         unset($querycache['time'][$cached]);
         unset($querycache['sql'][$cached]);
         unset($querycache['result'][$cached]);
         $cached = false;
} if ($cached) {
         return $querycache['result'][$cached];
} else {
         $result = mysql_query($sql);
         $querycache['sql'][] = $sql;
         $index = array_search($querycache['sql']);
         $querycache['time'][$index] = time();
         while ($r = assoc($result)) {
                $querycache['result'][$index][] = $r;
         }
         // save cache for other scripts
         file_put_contents('query.cache', serialize($querycache));
         return $querycache['result'][$index];
} } ... It's actually kind of a complicated process now that I'm thinking about it. -Micah On 03/13/2007 06:20 AM, Vincent wrote:
Hi all, I'm trying to improve the performance for visitors, and I want to know if there is some way to store the mySQL results of .php scripts in webpages. So, in example, when requesting http://www.mydomain.com/script.php?id=110 , it can be redirected to his cached version http://www.mydomain.com/id110.html. Any code or information would be appreciated, Regards,


« previous php.db (#43274) next »