Advertisement
HaLo2FrEeEk

Timed/On-Request Cache

Nov 27th, 2010
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1. <?php
  2. function fetchInfo($gt) {
  3.   $key = "********************************************";
  4.   $file = file_get_contents("http://www.bungie.net/api/reach/reachapijson.svc/player/details/nostats/".$key."/".$gt);
  5.   $json = json_decode($file, true);
  6.   unset($file, $key);
  7.  
  8.   $cache[] = time();
  9.   $cache[] = $json['Player']['armor_completion_percentage']; // Obvious
  10.   $cache[] = $json['Player']['commendation_completion_percentage']; // Obvious
  11.   $cache[] = $json['Player']['games_total']; // Games played
  12.   $cache[] = $json['Player']['service_tag']; // Obvious
  13.   $date = preg_match("#\/Date\((.+?)-(.+?)\)\/#i", $json['Player']['last_active'], $match);
  14.   $cache[] = $match[1] / 1000;
  15.  
  16.   return $cache;
  17.   }
  18.  
  19. $gt = "halo2freeek";
  20.  
  21. $cachefile = "./" . md5($gt) . "_cache.txt";
  22. $newCache = false;
  23. $newFile = false;
  24. if(file_exists($cachefile)) {
  25.   // The cache file exists, we need to read it's contents and
  26.   // Check if the timestamp is greater than one hour ago.
  27.   $file = file_get_contents($cachefile);
  28.   $cache = explode("#|#", $file);
  29.   unset($file);
  30.   $cacheTime = $cache[0];
  31.   $timeDiff = time() - $cacheTime;
  32.   if($timeDiff > 300) {
  33.     // It has been more than an hour since this cache file was created
  34.     // So we recreate it with updated information from the API
  35.  
  36.     echo "The cache file was older than 5 minutes, so we'll refresh it.<br><br>\n\n";
  37.  
  38.     $cache = fetchInfo($gt);
  39.     $newCacheTime = $cache[0];
  40.     $timeDiff = time() - $newCacheTime;
  41.     $newCache = true;
  42.     $str = implode("#|#", $cache);
  43.     file_put_contents($cachefile, $str);
  44.     unset($str);
  45.  
  46.     // We still have the $cache variable which has overwritten the
  47.     // Original and now holds NEW information.
  48.     } else {
  49.     // It has been less than an hour, so we're going to just read the
  50.     // Cache file's contents and use that information in our image
  51.  
  52.     echo "The cache file was younger than 5 minutes, so we'll use its contents.<br><br>\n\n";
  53.  
  54.     // We're just going to print the existing information now, for
  55.     // Example purposes.
  56.     }
  57.   } else {
  58.   // The cache file doesn't exist, so we'll need to grab the
  59.   // Information from the API and create a cache file.
  60.  
  61.   echo "The cache file doesn't exist, so we'll create it.<br><br>\n\n";
  62.  
  63.   $cache = fetchInfo($gt);
  64.   $newCacheTime = $cache[0];
  65.   $timeDiff = time() - $newCacheTime;
  66.   $newCache = true;
  67.   $newFile = true;
  68.   $str = implode("#|#", $cache);
  69.   file_put_contents($cachefile, $str);
  70.   unset($str);
  71.  
  72.   // We still have the $cache variable which has overwritten the
  73.   // Original and now holds NEW information.
  74.   }
  75.  
  76. echo "<pre>";
  77. foreach($cache as $key => $inf) {
  78.   switch($key) {
  79.     case 0:
  80.       echo "Cache Time: ";
  81.       break;
  82.     case 1:
  83.       echo "Armory Completion: ";
  84.       break;
  85.     case 2:
  86.       echo "Commendation Completion: ";
  87.       break;
  88.     case 3:
  89.       echo "Games Played: ";
  90.       break;
  91.     case 4:
  92.       echo "Service Tag: ";
  93.       break;
  94.     case 5:
  95.       echo "Last Active: ";
  96.       break;
  97.     }
  98.  
  99.   if($key != 5) {
  100.     echo $inf;
  101.     } else {
  102.     echo date($mydate, $inf);
  103.     }
  104.  
  105.   echo "\n";
  106.   }
  107.  
  108. echo "</pre>Raw Array: <br><br>";
  109.  
  110. echo "<pre>";
  111. print_r($cache);
  112. echo "</pre>";
  113.  
  114. echo "The current time is <b>" . date($mydate) . "</b> (" . time() . ")<br>\n";
  115. echo "The previous cache was " . (($newFile) ? " <b>never</b>.<br>\n" : "on <b>" . date($mydate, $cacheTime) . "</b> (" . $cacheTime . ")<br>\n");
  116. echo (($newCache) ? "The most recent cache was <b>right now.</b><br>\n" : "");
  117. echo "The difference between those times is <b>" . $timeDiff . "</b> seconds.<br>\n";
  118. echo "The next cache will occur after the page has been refreshed in <b>" . (300 - $timeDiff) . "</b> seconds.";
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement