Skip to content

Commit 6319507

Browse files
committed
feat: added cache logger
1 parent 70f6393 commit 6319507

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Config.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ public function __construct(array $options = [])
2121
{
2222
$resolver = new OptionsResolver();
2323
$this->configureOptions($resolver);
24-
2524
$this->options = $resolver->resolve($options);
2625

27-
$this->configureAdapters();
26+
$this->configureAware();
2827
}
2928

3029
private function configureOptions(OptionsResolver $resolver): void
@@ -53,7 +52,7 @@ private function configureOptions(OptionsResolver $resolver): void
5352
$resolver->setAllowedValues('language', Language::getList());
5453
}
5554

56-
private function configureAdapters(): void
55+
private function configureAware(): void
5756
{
5857
if ($this->getLogger() !== null) {
5958
$this->getHttpClientBuilder()->addPlugin(

src/Endpoint/AbstractEndpoint.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Psr\Http\Message\ResponseInterface;
2020
use Psr\Http\Message\StreamInterface;
2121
use Psr\Http\Message\UriInterface;
22+
use Psr\Log\LoggerInterface;
2223

2324
class AbstractEndpoint
2425
{
@@ -27,6 +28,8 @@ class AbstractEndpoint
2728

2829
private HttpMethodsClient $httpClient;
2930

31+
private ?LoggerInterface $logger;
32+
3033
private ?CacheItemPoolInterface $cache;
3134

3235
private bool $cacheInvalidation = false;
@@ -42,6 +45,7 @@ public function __construct(protected OpenWeatherMap $api)
4245
$config = $this->api->getConfig();
4346

4447
$this->httpClient = $config->getHttpClientBuilder()->getHttpClient();
48+
$this->logger = $config->getLogger();
4549
$this->cache = $config->getCache();
4650
$this->measurementSystem = $config->getMeasurementSystem();
4751
$this->language = $config->getLanguage();
@@ -70,15 +74,19 @@ protected function sendRequest(
7074
if ($this->cache !== null) {
7175
$cacheKey = $this->getCacheKey($uri);
7276

73-
// Invalidate cache (if exists) to force renewal
77+
// Invalidate cache to force new
7478
if ($this->cacheInvalidation === true) {
79+
$this->logger?->info('Cache invalidated', ['key' => $cacheKey]);
80+
7581
$this->cache->deleteItem($cacheKey);
7682
}
7783

7884
$cacheItem = $this->cache->getItem($cacheKey);
7985

80-
// If cache does not exist...
81-
if (!$cacheItem->isHit()) {
86+
if ($cacheItem->isHit()) {
87+
$this->logger?->info(\sprintf('Cache hit: %s %s', $method, $uri), ['key' => $cacheKey]);
88+
}
89+
else {
8290
$response = ResponseMediator::toArray(
8391
$this->handleRequest($method, $uri, $headers, $body)
8492
);
@@ -87,6 +95,8 @@ protected function sendRequest(
8795
$cacheItem->expiresAfter($this->cacheTtl);
8896

8997
$this->cache->save($cacheItem);
98+
99+
$this->logger?->info('Cached response', ['ttl' => $this->cacheTtl, 'key' => $cacheKey]);
90100
}
91101

92102
return $cacheItem->get();

0 commit comments

Comments
 (0)