//Pull data and cache for 15 minutes at a time
$query = (isset($_GET['query']) ? $_GET['query'] : '');
$apiHost = 'https://api.savings.com/rest/v1/deals.json';
$queryParams = array(
'developerKey' => 'bed54aa0-0368-4d51-912d-52d17cd096e8',
'pageSize' => 20 //Number of deals returned
);
$apiURL = $apiHost. '?'. http_build_query($queryParams);
$cacheKey = 'tct-savingsAPI-'. md5($apiURL);
$m = new Memcached();
$m->addServer('localhost', 11211);
if(!($jsonData = $m->get($cacheKey))){
if($m->getResultCode() == Memcached::RES_NOTFOUND){
foreach(array('EXCLUSIVE', 'PRINTABLE', 'FREE_SHIPPING', 'TOP_SCORE', 'NEWEST') as $query){
$jsonRequest = file_get_contents($apiURL. "&query=$query");
$jsonConvert = json_decode($jsonRequest);
$jsonData->deals = (object)array_merge((array)$jsonData->deals, (array)$jsonConvert->deals);
}
$m->set($cacheKey, $jsonData, time() + 60*15); //15 Minutes
}
}
?>