public function determineBucketRegionAsync($bucketName) { if ($cached = $this->cache->get($this->getCacheKey($bucketName))) { return Promise\promise_for($cached); } return $this->lookupBucketRegion($bucketName)->then(function ($region) use($bucketName) { $this->cache->set($this->getCacheKey($bucketName), $region); return $region; }); }
/** * Wraps a credential provider and saves provided credentials in an * instance of Aws\CacheInterface. Forwards calls when no credentials found * in cache and updates cache with the results. * * Defaults to using a simple file-based cache when none provided. * * @param callable $provider Credentials provider function to wrap * @param CacheInterface $cache (optional) Cache to store credentials * @param string|null $cacheKey (optional) Cache key to use * * @return callable */ public static function cache(callable $provider, CacheInterface $cache, $cacheKey = null) { $cacheKey = $cacheKey ?: 'aws_cached_credentials'; return function () use($provider, $cache, $cacheKey) { $found = $cache->get($cacheKey); if ($found instanceof CredentialsInterface && !$found->isExpired()) { return Promise\promise_for($found); } return $provider()->then(function (CredentialsInterface $creds) use($cache, $cacheKey) { $cache->set($cacheKey, $creds, null === $creds->getExpiration() ? 0 : $creds->getExpiration() - time()); return $creds; }); }; }