Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save asilbalaban/5988491 to your computer and use it in GitHub Desktop.
Save asilbalaban/5988491 to your computer and use it in GitHub Desktop.
Look for cache If cache file exist Return data from cache If data not cached Select data from database and cache Return data
function select ( $query )
{
$cacheFile = md5($query).'.html';
$cache = 'cache/'.$cacheFile;
$cacheSuresi = 60*60*24*7; # one week
if (time() - $cacheSuresi < @filemtime($cache))
{
$cacheOku = fopen($cache, 'r');
$sonuc = fgets($cacheOku);
fclose($cacheOku);
}
else
{
@unlink ($cache);
$sql = mysql_query($query);
$say = mysql_num_rows($sql);
if ( $say == 0 )
return false;
while ( $oku = mysql_fetch_assoc($sql) )
{
$bilgi[] = $oku;
}
$sonuc = serialize ( $bilgi );
$yaz = fopen($cache, "w+");
fwrite($yaz, $sonuc);
fclose($yaz);
}
return unserialize ( $sonuc );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment