wishthis/src/classes/cache/query.php
2022-10-12 16:49:05 +02:00

51 lines
1.1 KiB
PHP

<?php
/**
* Databse query cache
*/
namespace wishthis\Cache;
class Query extends Cache
{
/**
* Private
*/
private \wishthis\Database $database;
/**
* Public
*/
public function __construct(string $url, int $maxAge = \wishthis\Duration::YEAR)
{
global $database;
parent::__construct($url, $maxAge);
$this->directory .= '/query';
$this->database = $database;
}
public function get(): array
{
$filepath = $this->getFilepath();
$response = $this->exists() ? json_decode(file_get_contents($filepath), true) : array();
if (true === $this->generateCache()) {
$pdoStatement = $this->database->query($this->url);
if (false !== $pdoStatement) {
if (1 === $pdoStatement->rowCount()) {
$response = $pdoStatement->fetch();
} else {
$response = $pdoStatement->fetchAll();
}
}
$this->write($response);
}
return $response;
}
}