Skip to content

Commit

Permalink
Invalid cache before expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Oct 18, 2023
1 parent 997c3c0 commit 8815fa4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


4 changes: 3 additions & 1 deletion src/Driver/Cnb/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Driver/Ecb/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
}

}
6 changes: 4 additions & 2 deletions src/RatingList/RatingListCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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


/**
Expand Down Expand Up @@ -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');
}

Expand Down

0 comments on commit 8815fa4

Please sign in to comment.