db = dil_connect(); // remembers the database profiler_endSection(__METHOD__); } //// // Fetches data if it is cached (regardless of when it was cached) and null if it isn't cached. // Use fetchExpire() for expiring caches. //// function fetch($key){ profiler_beginSection(__METHOD__); $retVal = null; $oldestAllowed = date("Y-m-d H:i:s", $oldestAllowed); // convert the time to a format mySQL will understand $queryString = "SELECT pageData FROM pageCache WHERE keyName='$key'"; if($result = mysql_query($queryString,$this->db)){ if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){ $retVal = mysql_result($result, 0, "pageData"); } } profiler_endSection(__METHOD__); return $retVal; } // end fetch() //// // Fetches data if it is cached and up to date. The validAfter var is the time which the cache would have had // to be made more recently than in order to not be expired. An easy way to generate this value is with strtotime. // Eg: strtotime('-2 hour'); // only uses the cache if it is 2 hours old or less. // // If the value is not cached or is expired, the value returned will be null. //// function fetchExpire($key, $validAfter){ profiler_beginSection(__METHOD__); $retVal = null; $validAfter = date("Y-m-d H:i:s", $validAfter); // convert the time to a format mySQL will understand $queryString = "SELECT pageData FROM pageCache WHERE keyName='$key' AND updatedAt >= '$validAfter'"; if($result = mysql_query($queryString,$this->db)){ if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){ $retVal = mysql_result($result, 0, "pageData"); } } profiler_endSection(__METHOD__); return $retVal; } // end fetchExpire() //// // Stores a value in the cache (it will be automatically timestamped). //// function cacheValue($key, $source){ profiler_beginSection(__METHOD__); $source = str_replace("'", "\'", $source); // to prevent query errors $queryString = "REPLACE INTO pageCache (keyName, pageData) VALUES ('$key', '$source')"; $retVal = mysql_query($queryString, $this->db); profiler_endSection(__METHOD__); return $retVal; } // end cacheValue() } // end class Cache ?>