public async Task <ISecret> GetSecretObjectAsync(string secretName, ILogger logger) { if (string.IsNullOrEmpty(secretName)) { throw new ArgumentException("Null or empty secret name", nameof(secretName)); } // If the cache contains the secret and it is not expired, return the cached value. if (_cache.TryGetValue(secretName, out CachedSecret result) && !IsSecretOutdated(result)) { return(result.Secret); } var start = DateTimeOffset.UtcNow; // The cache does not contain a fresh copy of the secret. Fetch and cache the secret. var updatedValue = new CachedSecret(await _internalReader.GetSecretObjectAsync(secretName)); var updatedSecret = _cache.AddOrUpdate(secretName, updatedValue, (key, old) => updatedValue).Secret; logger?.LogInformation("Refreshed secret {SecretName}, Expiring at: {ExpirationTime}. Took {ElapsedMilliseconds}ms.", updatedSecret.Name, updatedSecret.Expiration == null ? "null" : ((DateTimeOffset)updatedSecret.Expiration).UtcDateTime.ToString(), (DateTimeOffset.UtcNow - start).TotalMilliseconds.ToString("F2")); return(updatedSecret); }
public async Task <ISecret> GetSecretObjectAsync(string secretName) { if (string.IsNullOrEmpty(secretName)) { throw new ArgumentException("Null or empty secret name", nameof(secretName)); } // If the cache contains the secret and it is not expired, return the cached value. if (_cache.TryGetValue(secretName, out CachedSecret result) && !IsSecretOutdated(result)) { return(result.Secret); } // The cache does not contain a fresh copy of the secret. Fetch and cache the secret. var updatedValue = new CachedSecret(await _internalReader.GetSecretObjectAsync(secretName)); return(_cache.AddOrUpdate(secretName, updatedValue, (key, old) => updatedValue).Secret); }
private bool IsSecretOutdated(CachedSecret cachedSecret) { return(((DateTime.UtcNow - cachedSecret.CacheTime) >= _refreshInterval) || (cachedSecret.Secret.Expiration != null && (cachedSecret.Secret.Expiration - DateTime.UtcNow) <= _refreshIntervalBeforeExpiry)); }
private bool IsSecretOutdated(CachedSecret secret) { return((DateTime.UtcNow - secret.CacheTime) >= _refreshInterval); }