Skip to content

Commit cff7a69

Browse files
authored
Ignore cache errors (#892)
1 parent b4d0531 commit cff7a69

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/Credentials/PsrCacheProvider.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace AsyncAws\Core\Credentials;
66

77
use AsyncAws\Core\Configuration;
8+
use Psr\Cache\CacheException;
89
use Psr\Cache\CacheItemPoolInterface;
10+
use Psr\Log\LoggerInterface;
911

1012
/**
1113
* Cache the Credential generated by the decorated CredentialProvider with PSR-6.
@@ -20,13 +22,32 @@ final class PsrCacheProvider implements CredentialProvider
2022

2123
private $decorated;
2224

23-
public function __construct(CredentialProvider $decorated, CacheItemPoolInterface $cache)
25+
private $logger;
26+
27+
public function __construct(CredentialProvider $decorated, CacheItemPoolInterface $cache, ?LoggerInterface $logger = null)
2428
{
2529
$this->decorated = $decorated;
2630
$this->cache = $cache;
31+
$this->logger = $logger;
2732
}
2833

2934
public function getCredentials(Configuration $configuration): ?Credentials
35+
{
36+
try {
37+
return $this->getFromCache($configuration);
38+
} catch (CacheException $e) {
39+
if (null !== $this->logger) {
40+
$this->logger->error('Failed to get AWS credentials from cache.', ['exception' => $e]);
41+
}
42+
43+
return $this->decorated->getCredentials($configuration);
44+
}
45+
}
46+
47+
/**
48+
* @throws \Psr\Cache\CacheException
49+
*/
50+
private function getFromCache(Configuration $configuration): ?Credentials
3051
{
3152
$item = $this->cache->getItem('AsyncAws.Credentials.' . sha1(\serialize([$configuration, \get_class($this->decorated)])));
3253
if (!$item->isHit()) {

src/Credentials/SymfonyCacheProvider.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace AsyncAws\Core\Credentials;
66

77
use AsyncAws\Core\Configuration;
8+
use Psr\Cache\CacheException;
9+
use Psr\Log\LoggerInterface;
810
use Symfony\Contracts\Cache\CacheInterface;
911
use Symfony\Contracts\Cache\ItemInterface;
1012

@@ -23,16 +25,20 @@ final class SymfonyCacheProvider implements CredentialProvider
2325

2426
private $decorated;
2527

26-
public function __construct(CredentialProvider $decorated, CacheInterface $cache)
28+
private $logger;
29+
30+
public function __construct(CredentialProvider $decorated, CacheInterface $cache, ?LoggerInterface $logger = null)
2731
{
2832
$this->decorated = $decorated;
2933
$this->cache = $cache;
34+
$this->logger = $logger;
3035
}
3136

3237
public function getCredentials(Configuration $configuration): ?Credentials
3338
{
34-
return $this->cache->get('AsyncAws.Credentials.' . sha1(\serialize([$configuration, \get_class($this->decorated)])), function (ItemInterface $item) use ($configuration) {
35-
$credential = $this->decorated->getCredentials($configuration);
39+
$provider = $this->decorated;
40+
$closure = \Closure::fromCallable(static function (ItemInterface $item) use ($configuration, $provider) {
41+
$credential = $provider->getCredentials($configuration);
3642

3743
if (null !== $credential && null !== $exp = $credential->getExpireDate()) {
3844
$item->expiresAt($exp);
@@ -42,5 +48,15 @@ public function getCredentials(Configuration $configuration): ?Credentials
4248

4349
return $credential;
4450
});
51+
52+
try {
53+
return $this->cache->get('AsyncAws.Credentials.' . sha1(\serialize([$configuration, \get_class($this->decorated)])), $closure);
54+
} catch (CacheException $e) {
55+
if (null !== $this->logger) {
56+
$this->logger->error('Failed to get AWS credentials from cache.', ['exception' => $e]);
57+
}
58+
59+
return $provider->getCredentials($configuration);
60+
}
4561
}
4662
}

0 commit comments

Comments
 (0)