Reduce statistics cache

This commit is contained in:
grandeljay 2022-10-12 16:49:05 +02:00
parent 36246c2645
commit 03cd042ac7
6 changed files with 27 additions and 9 deletions

View file

@ -30,7 +30,8 @@ switch ($_SERVER['REQUEST_METHOD']) {
foreach ($tables as $table) { foreach ($tables as $table) {
$count = new Cache\Query( $count = new Cache\Query(
'SELECT COUNT(`id`) AS "count" 'SELECT COUNT(`id`) AS "count"
FROM `' . $table . '`;' FROM `' . $table . '`;',
Duration::DAY
); );
$response['data'][$table] = $count->get(); $response['data'][$table] = $count->get();

View file

@ -21,10 +21,9 @@ class Blog extends Cache
*/ */
public function __construct($url) public function __construct($url)
{ {
parent::__construct($url); parent::__construct($url, \wishthis\Duration::DAY);
$this->directory .= '/blog'; $this->directory .= '/blog';
$this->maxAge = 86400; // 24 hours
} }
public function get(): \stdClass|array public function get(): \stdClass|array

View file

@ -8,12 +8,16 @@ namespace wishthis\Cache;
class Cache class Cache
{ {
/**
* Private
*/
private int $maxAge;
/** /**
* Protected * Protected
*/ */
protected string $url; protected string $url;
protected string $directory = ROOT . '/src/cache'; protected string $directory = ROOT . '/src/cache';
protected int $maxAge = 2592000; // 30 days
protected function getAge(): int protected function getAge(): int
{ {
@ -50,9 +54,10 @@ class Cache
/** /**
* Public * Public
*/ */
public function __construct($url) public function __construct(string $url, int $maxAge = \wishthis\Duration::YEAR)
{ {
$this->url = trim($url); $this->url = trim($url);
$this->maxAge = $maxAge;
} }
public function exists(): bool public function exists(): bool

View file

@ -25,7 +25,7 @@ class Embed extends Cache
*/ */
public function __construct($url) public function __construct($url)
{ {
parent::__construct($url); parent::__construct($url, \wishthis\Duration::MONTH);
$this->directory .= '/embed'; $this->directory .= '/embed';
} }

View file

@ -16,11 +16,11 @@ class Query extends Cache
/** /**
* Public * Public
*/ */
public function __construct($url) public function __construct(string $url, int $maxAge = \wishthis\Duration::YEAR)
{ {
global $database; global $database;
parent::__construct($url); parent::__construct($url, $maxAge);
$this->directory .= '/query'; $this->directory .= '/query';
$this->database = $database; $this->database = $database;

13
src/classes/duration.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace wishthis;
class Duration
{
public const HOUR = 3600;
public const DAY = self::HOUR * 24;
public const WEEK = self::DAY * 7;
public const MONTH = self::DAY * 30;
public const QUARTER = self::MONTH * 3;
public const YEAR = self::MONTH * 12;
}