Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
More Jobs
Recent
Code to detect the web browser
Base class for easy class property handling
Your average file filter...
simplify multiple gsub
Parse XML from Yahoo weather RSS feed
Base64 image encoding and image hash
Query TinyUrl the fastest way
Bencode decoder
model loader
Link to if else simple condition.
Popular
simplify multiple gsub
Code to detect the web browser
Your average file filter...
Query TinyUrl the fastest way
Base64 image encoding and image hash
Parse XML from Yahoo weather RSS feed
Base class for easy class property handling
Default value for user column
Method for sorting
Code transactions
Pastable version of
DB Cache
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62</pre> <pre class="sunburst">function db_cache_2($query) { // Query cache table $sql = 'SELECT * FROM `query_cache` WHERE `query` = \''.mysql_escape_string($query).'\' LIMIT 1'; $qry = mysql_query($sql) or die('Cache Query Error::: '.mysql_error()); // No cache if(mysql_num_rows($qry) < 1) { $qry2 = mysql_query($query) or die('Query error'); $i = 0; while($row = mysql_fetch_assoc($qry2)) { foreach($row as $key=>$val) { $arr[$i][$key] = $val; } ++$i; } $sql2 = 'INSERT INTO `query_cache` (`cached`, `query`, `results`) VALUES (now(), \''.mysql_escape_string($query).'\', \''.serialize($arr).'\')'; mysql_query($sql2) or die('Cache Insert Error'); } else { // Cache avail $row = mysql_fetch_assoc($qry); // Check the file updates $sql2 = 'SELECT `updated` FROM `ftp_updates` ORDER BY `updated` DESC LIMIT 1'; $qry2 = mysql_query($sql2) or die('Cache Compare Query Error::: '.mysql_error()); $row2 = mysql_fetch_row($qry2); // Out of date - Update the cache if(convert_datetime($row['cached']) < convert_datetime($row2[0])) { $qry3 = mysql_query($query) or die('Query error'); $i = 0; while($row3 = mysql_fetch_assoc($qry3)) { foreach($row3 as $key=>$val) { $arr[$i][$key] = $val; } ++$i; } $sql2 = 'UPDATE `query_cache` SET `cached` = now(), `query` = \''.mysql_escape_string($query).'\', `results` = \''.serialize($arr).'\' WHERE `id` = \''.$row['id'].'\''; mysql_query($sql2) or die('Cache Update Error'); } else { // Cache Valid // Would have put this above the if - but I think the unserialize would slow things down more than the else statement would. // Plus I would have to blank out the arr to make sure the array was correct in the end (No old data). $arr = unserialize($row['results']); } } return($arr); } </pre> </div> </div> <a href="http://refactormycode.com/codes/213-db-cache" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>