1 | <?php |
||
2 | |||
3 | require_once __DIR__ . "/../vendor/autoload.php"; |
||
4 | |||
5 | use Silviooosilva\CacheerPhp\Cacheer; |
||
6 | |||
7 | $options = [ |
||
8 | "cacheDir" => __DIR__ . "/cache", |
||
9 | ]; |
||
10 | |||
11 | $Cacheer = new Cacheer($options); |
||
12 | |||
13 | // URL da API e chave de cache |
||
14 | $apiUrl = 'https://jsonplaceholder.typicode.com/posts'; |
||
15 | $cacheKey = 'api_response_' . md5($apiUrl); |
||
16 | |||
17 | // Verificando se a resposta da API já está no cache |
||
18 | $cachedResponse = $Cacheer->getCache($cacheKey); |
||
19 | |||
20 | if ($Cacheer->isSuccess()) { |
||
21 | // Use a resposta do cache |
||
22 | $response = $cachedResponse; |
||
23 | } else { |
||
24 | // Faça a chamada à API e armazene a resposta no cache |
||
25 | $response = file_get_contents($apiUrl); |
||
26 | $Cacheer->putCache($cacheKey, $response); |
||
27 | } |
||
28 | |||
29 | // Usando a resposta da API (do cache ou da chamada) |
||
30 | $data = json_decode($response, true); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
31 | print_r($data); |
||
32 |