Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // So essentially i'm storing curl results into a memcache key to track its value
- function roundToNextHour() {
- // i found this function on stackoverflow ( who doesnt ! )
- $date = new DateTime( );
- $minutes = $date->format('i');
- if($minutes > 0){
- $date->modify("+1 hour");
- $date->modify('-'.$minutes.' minutes');
- }
- return $date;
- }
- function updateMemcache($curlInfo){
- // $curlinfo is the result of a previous call to
- // $curlinfo = curl_getinfo($ch);
- // following a curl_exec($ch) call.
- // write_log("updateMemCache : begins " );
- $begin = new DateTime( );
- $future= roundToNextHour( );
- $Memcached = new Memcached();
- $Memcached->addServer('localhost', 11211);
- $seconds = $future->getTimestamp() - $begin->getTimestamp();
- $memObj=array(
- "count"=>0,
- "lastResponse"=>0,
- "longResponse"=>0,
- "longCount"=>0,
- "keySet"=>$begin->format("Y/M/d H:i:s" )
- );
- if($Memcached->get("curl_counts")){
- // write_log("updateMemCache : seconds to future " . $seconds);
- // write_log("updateMemCache : memObj " . var_export($memObj,true));
- // write_log("updateMemCache : Possibly got the memObj");
- $memObj=$Memcached->get("curl_counts");
- $memObj["count"]++;
- $memObj["lastResponse"]=$curlInfo["total_time"];
- if($memObj["lastResponse"]>$memObj["longResponse"])$memObj["longResponse"] = $memObj["lastResponse"];
- if($memObj["lastResponse"]>=1){
- $memObj["longCount"]++;
- }
- // update key
- if($Memcached->replace("curl_counts",$memObj)){
- // write_log("updateMemCache : seconds to future " . $seconds);
- // write_log("updateMemCache : memObj " . var_export($memObj,true));
- // write_log("updateMemCache : Possibly Replaced the memObj");
- }else{
- // write_log("updateMemCache : seconds to future " . $seconds);
- // write_log("updateMemCache : memObj " . var_export($memObj,true));
- // write_log("updateMemCache : Failed to Replace the memObj");
- }
- }else{
- // set key for first time
- // write_log("updateMemCache : Failed to Get memObj " . var_export($memObj,true));
- if($Memcached->set("curl_counts",$memObj, (time() + $seconds + 60))){
- // write_log("updateMemCache : seconds to future " . $seconds);
- // write_log("updateMemCache : memObj " . var_export($memObj,true));
- // write_log("updateMemCache : Possibly Set the memObj");
- }else{
- // write_log("updateMemCache : Failed to Set memObj " . var_export($memObj,true));
- }
- }
- unset($Memcached);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement