Advertisement
loudmouthman

Storing some curl info into memcache

Feb 17th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. // So essentially i'm storing curl results into a memcache key to track its value
  2.  
  3.  
  4. function roundToNextHour() {
  5.     // i found this function on stackoverflow ( who doesnt ! )
  6.     $date = new DateTime(  );
  7.     $minutes = $date->format('i');
  8.     if($minutes > 0){
  9.         $date->modify("+1 hour");
  10.         $date->modify('-'.$minutes.' minutes');
  11.     }
  12.     return $date;
  13. }
  14.  
  15. function updateMemcache($curlInfo){
  16. // $curlinfo is the result of a previous call to
  17. //  $curlinfo = curl_getinfo($ch);
  18. // following a curl_exec($ch) call.
  19. //      write_log("updateMemCache : begins " );
  20.         $begin = new DateTime( );
  21.         $future=  roundToNextHour( );
  22.         $Memcached = new Memcached();
  23.         $Memcached->addServer('localhost', 11211);
  24.         $seconds = $future->getTimestamp() - $begin->getTimestamp();
  25.         $memObj=array(
  26.                         "count"=>0,
  27.                         "lastResponse"=>0,
  28.                         "longResponse"=>0,
  29.                         "longCount"=>0,
  30.                         "keySet"=>$begin->format("Y/M/d H:i:s"  )
  31.                         );
  32.  
  33.  
  34.         if($Memcached->get("curl_counts")){
  35. //              write_log("updateMemCache : seconds to future   " . $seconds);
  36. //              write_log("updateMemCache : memObj   " . var_export($memObj,true));
  37. //              write_log("updateMemCache : Possibly got the memObj");
  38.                 $memObj=$Memcached->get("curl_counts");
  39.                 $memObj["count"]++;
  40.                 $memObj["lastResponse"]=$curlInfo["total_time"];
  41.                 if($memObj["lastResponse"]>$memObj["longResponse"])$memObj["longResponse"] = $memObj["lastResponse"];
  42.                 if($memObj["lastResponse"]>=1){
  43.                         $memObj["longCount"]++;
  44.                         }
  45.                         // update key
  46.                         if($Memcached->replace("curl_counts",$memObj)){
  47. //                              write_log("updateMemCache : seconds to future   " . $seconds);
  48. //                              write_log("updateMemCache : memObj   " . var_export($memObj,true));
  49. //                              write_log("updateMemCache : Possibly Replaced the memObj");
  50.                                 }else{
  51. //                              write_log("updateMemCache : seconds to future   " . $seconds);
  52. //                              write_log("updateMemCache : memObj   " . var_export($memObj,true));
  53. //                              write_log("updateMemCache : Failed to Replace the memObj");
  54.                                 }
  55.                 }else{
  56.                         // set key for first time
  57. //                      write_log("updateMemCache : Failed to Get memObj   " . var_export($memObj,true));      
  58.                 if($Memcached->set("curl_counts",$memObj, (time() +  $seconds + 60))){
  59. //                      write_log("updateMemCache : seconds to future   " . $seconds);
  60. //                      write_log("updateMemCache : memObj   " . var_export($memObj,true));
  61. //                      write_log("updateMemCache : Possibly Set the memObj");
  62.                 }else{
  63. //                      write_log("updateMemCache : Failed to Set memObj   " . var_export($memObj,true));
  64.                 }
  65.  
  66.  
  67.                 }
  68.  
  69.  
  70.         unset($Memcached);
  71.         return true;
  72.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement