cache method added

This commit is contained in:
Olu Amey
2021-10-28 08:02:01 -04:00
parent 650d7c92b5
commit af11c58652
+22 -11
View File
@@ -8,18 +8,29 @@ class WpContentsClient
public static function serviceGetBlogItems($apiEndpointsUrl) public static function serviceGetBlogItems($apiEndpointsUrl)
{ {
$client = new HTTPClient(); $cache = \Config\Services::cache();
try {
$response = $client->request( if (!$cache->get('BlogWebData')){
'GET', $client = new HTTPClient();
"{$apiEndpointsUrl}/wordpress-data",
['connect_timeout' => 2, 'timeout' => 3, 'debug' => false] try {
); $response = $client->request(
} catch (Exception $e) { 'GET',
// echo "\n".$e->getMessage()."\n"; "{$apiEndpointsUrl}/wordpress-data",
$response= []; // empty array ['connect_timeout' => 2, 'timeout' => 3, 'debug' => false]
);
} catch (Exception $e) {
// echo "\n".$e->getMessage()."\n";
$response= []; // empty array
}
$responseArray = json_decode($response->getBody());
$cache->save('BlogWebData',$responseArray,600);
} }
return json_decode($response->getBody()); else{
$responseArray = $cache->get('BlogWebData');
}
//var_dump($responseArray);
return $responseArray;
} }
} }