Reduce statistics cache
This commit is contained in:
parent
36246c2645
commit
03cd042ac7
6 changed files with 27 additions and 9 deletions
|
@ -30,7 +30,8 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
foreach ($tables as $table) {
|
||||
$count = new Cache\Query(
|
||||
'SELECT COUNT(`id`) AS "count"
|
||||
FROM `' . $table . '`;'
|
||||
FROM `' . $table . '`;',
|
||||
Duration::DAY
|
||||
);
|
||||
|
||||
$response['data'][$table] = $count->get();
|
||||
|
|
3
src/classes/cache/blog.php
vendored
3
src/classes/cache/blog.php
vendored
|
@ -21,10 +21,9 @@ class Blog extends Cache
|
|||
*/
|
||||
public function __construct($url)
|
||||
{
|
||||
parent::__construct($url);
|
||||
parent::__construct($url, \wishthis\Duration::DAY);
|
||||
|
||||
$this->directory .= '/blog';
|
||||
$this->maxAge = 86400; // 24 hours
|
||||
}
|
||||
|
||||
public function get(): \stdClass|array
|
||||
|
|
11
src/classes/cache/cache.php
vendored
11
src/classes/cache/cache.php
vendored
|
@ -8,12 +8,16 @@ namespace wishthis\Cache;
|
|||
|
||||
class Cache
|
||||
{
|
||||
/**
|
||||
* Private
|
||||
*/
|
||||
private int $maxAge;
|
||||
|
||||
/**
|
||||
* Protected
|
||||
*/
|
||||
protected string $url;
|
||||
protected string $directory = ROOT . '/src/cache';
|
||||
protected int $maxAge = 2592000; // 30 days
|
||||
|
||||
protected function getAge(): int
|
||||
{
|
||||
|
@ -50,9 +54,10 @@ class Cache
|
|||
/**
|
||||
* 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
|
||||
|
|
2
src/classes/cache/embed.php
vendored
2
src/classes/cache/embed.php
vendored
|
@ -25,7 +25,7 @@ class Embed extends Cache
|
|||
*/
|
||||
public function __construct($url)
|
||||
{
|
||||
parent::__construct($url);
|
||||
parent::__construct($url, \wishthis\Duration::MONTH);
|
||||
|
||||
$this->directory .= '/embed';
|
||||
}
|
||||
|
|
4
src/classes/cache/query.php
vendored
4
src/classes/cache/query.php
vendored
|
@ -16,11 +16,11 @@ class Query extends Cache
|
|||
/**
|
||||
* Public
|
||||
*/
|
||||
public function __construct($url)
|
||||
public function __construct(string $url, int $maxAge = \wishthis\Duration::YEAR)
|
||||
{
|
||||
global $database;
|
||||
|
||||
parent::__construct($url);
|
||||
parent::__construct($url, $maxAge);
|
||||
|
||||
$this->directory .= '/query';
|
||||
$this->database = $database;
|
||||
|
|
13
src/classes/duration.php
Normal file
13
src/classes/duration.php
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue