Skip to content

Commit

Permalink
fixed cache key bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Aug 25, 2024
1 parent f257ef8 commit d010409
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/ExchangeRateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function currencyRatioBetween(DateTime $dateTime, Currency $baseCurrency,
*/
public function getExchangeRates(DateTime $dateTime): array
{
$cacheKey = sprintf('cnb-exchange-rates-%s', DateConvert::fromDateTime($dateTime));
$dateTimeFormatted = DateConvert::fromDateTime($dateTime);

$cachedValue = $this->cacheItemPool->getItem($cacheKey);
$cachedValue = $this->cacheItemPool->getItem($dateTimeFormatted);

if ($cachedValue->isHit()) {
/** @var array<string, array{0: float, 1: float}> */
Expand All @@ -79,16 +79,16 @@ public function getExchangeRates(DateTime $dateTime): array

$todayDateTime = new DateTime();
$todayDateTimeKey = DateConvert::fromDateTime($todayDateTime);
if ($dateTime > $todayDateTime && $todayDateTimeKey !== $cacheKey) {
if ($dateTime > $todayDateTime && $todayDateTimeKey !== $dateTimeFormatted) {
throw new RuntimeException('Cannot retrieve future exchange rates!');
}

$isToday = $todayDateTimeKey === $cacheKey;
$isToday = $todayDateTimeKey === $dateTimeFormatted;

// If it's today, cache for 5 minutes, if it's in the past, cache indefinitely...
$cachedValue->expiresAfter($isToday ? 300 : null);

$ratios = $this->fetchRatiosByKey($cacheKey);
$ratios = $this->fetchRatiosByKey($dateTimeFormatted);

$cachedValue->set($ratios);

Expand Down
15 changes: 8 additions & 7 deletions tests/ExchangeRateProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use Money\Currency;
use Money\Money;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientExceptionInterface;
use Zrnik\Exchange\CnbExchange;
use Zrnik\Exchange\ExchangeRateProvider;
use \Psr\Cache\InvalidArgumentException;

class ExchangeRateProviderTest extends TestCase
{
/**
* @throws \Psr\Cache\InvalidArgumentException
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws InvalidArgumentException
* @throws ClientExceptionInterface
*/
public function testGetExchangeRate(): void
{
Expand All @@ -38,8 +40,8 @@ public function testGetExchangeRate(): void
$this->assertSame(
[
'CZK' => [1, 1],
'USD' => [1, 23.454],
'EUR' => [1, 25.37],
'USD' => [1, 22.621],
'EUR' => [1, 25.41],
],
$exampleRates,
);
Expand All @@ -60,7 +62,7 @@ public function testGetExchangeRate(): void
);

$this->assertEquals(
26893,
26313,
$converter->convert($USD_345, new Currency('GBP'))->getAmount()
);

Expand All @@ -75,7 +77,6 @@ public function testGetExchangeRate(): void
new Currency("CZK"),
);

$this->assertSame($ratio, 25.37);

$this->assertSame($ratio, 26.465);
}
}

0 comments on commit d010409

Please sign in to comment.