Cache statistics

This commit is contained in:
grandeljay 2022-06-10 16:58:16 +02:00
parent e256774c85
commit 64a1d19518
2 changed files with 52 additions and 5 deletions

View file

@ -6,6 +6,8 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me> * @author Jay Trees <github.jay@grandel.anonaddy.me>
*/ */
namespace wishthis;
$api = true; $api = true;
$response = array(); $response = array();
@ -26,12 +28,12 @@ switch ($_SERVER['REQUEST_METHOD']) {
$response['data'] = array(); $response['data'] = array();
foreach ($tables as $table) { foreach ($tables as $table) {
$count = $database $count = new Cache\Query(
->query('SELECT COUNT(`id`) AS "count" 'SELECT COUNT(`id`) AS "count"
FROM `' . $table . '`;') FROM `' . $table . '`;'
->fetch(); );
$response['data'][$table] = $count; $response['data'][$table] = $count->get();
} }
} else { } else {
$count = $database $count = $database

45
src/classes/cache/query.php vendored Normal file
View file

@ -0,0 +1,45 @@
<?php
/**
* Databse query cache
*/
namespace wishthis\Cache;
class Query extends Cache
{
private Database $databse;
public function __construct($url)
{
global $database;
parent::__construct($url);
$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;
}
}