Example #1
0
        private AsyncLazy <TValue> GetOrAddExpirableAsyncLazy <TKey, TValue>(
            VolatileMap <TKey, AsyncLazy <TValue> > map,
            TKey key,
            TimeSpan timeToLive,
            Func <Task <TValue> > func)
        {
            AsyncLazy <TValue> asyncLazyValue;

            while (!map.TryGetValue(key, out asyncLazyValue))
            {
                bool invalidate()
                {
                    map.Invalidate(key);
                    return(false);
                }

                asyncLazyValue = new AsyncLazy <TValue>(async() =>
                {
                    try
                    {
                        return(await func());
                    }
                    catch (Exception) when(invalidate())
                    {
                        // This should never be reached.
                        // Using Exception filter to invalidate entry on exception
                        // and preserve stack trace
                        throw;
                    }
                });
                map.TryAdd(key, asyncLazyValue, timeToLive);
            }

            return(asyncLazyValue);
        }
Example #2
0
        public async Task <X509Certificate2> GetCertificateAsync(string certificateName)
        {
            if (_cachedCertificates.TryGetValue(certificateName, out var certificate))
            {
                return(certificate);
            }

            var client = new SecretClient(_uri, _azureCreds);

            IcmCallsCounter.Increment();

            var certResponse = await client.GetSecretAsync(certificateName);

            var bytes = Convert.FromBase64String(certResponse.Value.Value);
            var cert  = new X509Certificate2(bytes);

            var added = _cachedCertificates.TryAdd(certificateName, cert, _cacheTimeToLive, replaceIfExists: true);

            Contract.Assert(added, "Failed to add certificate to cache.");

            return(cert);
        }