From 8815fa49e38f8f079e55bb60a7b76c054d891f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Wed, 18 Oct 2023 12:28:04 +0200 Subject: [PATCH] Invalid cache before expiration --- README.md | 16 ++++++++++++++++ src/Driver/Cnb/Day.php | 4 +++- src/Driver/Ecb/Day.php | 4 +++- src/RatingList/RatingListCache.php | 6 ++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4b0ba05..a5c76cd 100644 --- a/README.md +++ b/README.md @@ -81,3 +81,19 @@ foreach ($exchange as $code => $property) { var_dump($property); } ``` + +## Caching + +The cache invalid automatic at some time, defined by property `Driver::$refresh`. From this property is counted time to live. Little better is invalid cache by cron. Because one request on server does not lock other requests. Let's run cron max. 15 minutes before invalidate cache. +```php +use h4kuna\Exchange\RatingList\RatingListCache; +use h4kuna\Exchange\RatingList\CacheEntity; +use h4kuna\Exchange\Driver\Cnb\Day; + +/** @var RatingListCache $ratingListCache */ +$ratingListCache->rebuild(new CacheEntity(null, Day::class)); +``` + +In example, is used `h4kuna\Exchange\Driver\Cnb\Day::$refresh` is defined at 15:00. Run cron 14:55 every day. + + diff --git a/src/Driver/Cnb/Day.php b/src/Driver/Cnb/Day.php index 022e787..9472e8e 100644 --- a/src/Driver/Cnb/Day.php +++ b/src/Driver/Cnb/Day.php @@ -12,6 +12,8 @@ class Day extends Exchange\Driver\Driver { // private const URL_DAY_OTHER = 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_ostatnich_men/kurzy.txt'; + public static string $url = 'https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt'; + protected string $refresh = 'today 15:00:00'; protected string $timeZone = 'Europe/Prague'; @@ -47,7 +49,7 @@ protected function createProperty($row): Property protected function prepareUrl(?\DateTimeInterface $date): string { - $url = 'https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt'; + $url = self::$url; if ($date === null) { return $url; diff --git a/src/Driver/Ecb/Day.php b/src/Driver/Ecb/Day.php index cd5f7f8..2a5f2fb 100644 --- a/src/Driver/Ecb/Day.php +++ b/src/Driver/Ecb/Day.php @@ -12,6 +12,8 @@ class Day extends Exchange\Driver\Driver { protected string $timeZone = 'Europe/Berlin'; + public static string $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; + /** * @return iterable<\SimpleXMLElement> @@ -55,7 +57,7 @@ protected function prepareUrl(?\DateTimeInterface $date): string throw new Exchange\Exceptions\InvalidStateException('Ecb does not support history.'); } - return 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; + return self::$url; } } diff --git a/src/RatingList/RatingListCache.php b/src/RatingList/RatingListCache.php index 58f3cae..9d6122a 100644 --- a/src/RatingList/RatingListCache.php +++ b/src/RatingList/RatingListCache.php @@ -16,7 +16,9 @@ final class RatingListCache { - public int $floatTtl = 600; + public int $floatTtl = 600; // seconds -> 10 minutes + + private const BEFORE_EXPIRATION_CACHE = 900; // seconds -> 15 minutes /** @@ -110,7 +112,7 @@ private function buildCache(CacheEntity $cacheEntity, CacheInterface $cache, str private static function countTTL(DateTime $dateTime): int { - if ($dateTime->getTimestamp() < time()) { + if ($dateTime->getTimestamp() < (time() - self::BEFORE_EXPIRATION_CACHE)) { $dateTime->modify('+1 day'); }