55namespace AsyncAws \Core \Credentials ;
66
77use AsyncAws \Core \Configuration ;
8+ use Psr \Cache \CacheException ;
9+ use Psr \Log \LoggerInterface ;
810use Symfony \Contracts \Cache \CacheInterface ;
911use 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