public bool TryGetValue <T>(string key, out T value) { var fullKey = CacheKeyHelper.GetFullKey(_prefix, key); using var activity = _activitySource.CreateStartedActivity(nameof(TryGetValue), BuildTags(CacheEvents.Miss, fullKey)); bool isCached; (isCached, value) = _executor.TryExecute(() => { var result = Instance.TryGetValue(fullKey, out T obj); return(result, obj); }); if (!isCached) { _logger.LogMissed(BuildTarget(nameof(TryGetValue)), fullKey, _logSensitive); return(false); } _logger.LogHit(BuildTarget(nameof(TryGetValue)), fullKey, value !, _logSensitive); activity.SetEvent(CacheEvents.Hit); return(true); }
public void Remove(string key) { var fullKey = CacheKeyHelper.GetFullKey(_prefix, key); using var activity = _activitySource.CreateStartedActivity(nameof(Remove), BuildTags(CacheEvents.Remove, fullKey)); _executor.TryExecute(() => Instance.Remove(fullKey)); _logger.LogRemoved(BuildTarget(nameof(Remove)), key, _logSensitive); }
public async Task <T?> GetAsync <T>(string key, CancellationToken cancellationToken = default) { var fullKey = CacheKeyHelper.GetFullKey(_prefix, key); using var activity = _activitySource.CreateStartedActivity(nameof(GetAsync), BuildTags(CacheEvents.Miss, fullKey)); var cached = await _executor.TryExecuteAsync(async() => (await Instance.GetAsync(fullKey, cancellationToken)).AsMemory()); if (cached.IsEmpty) { _logger.LogMissed(BuildTarget(nameof(GetAsync)), fullKey, _logSensitive); return(default);
public void Set <T>(string key, T value, MemoryCacheEntryOptions options) { var fullKey = CacheKeyHelper.GetFullKey(_prefix, key); using var activity = _activitySource.CreateStartedActivity(nameof(Set), BuildTags(CacheEvents.Skipped, fullKey)); if (Utils.IsDefaultStruct(value)) { _logger.LogNotSet(BuildTarget(nameof(Set)), fullKey, value !, _logSensitive); return; } _executor.TryExecute(() => { using var entry = Instance.CreateEntry(fullKey); entry.SetOptions(options); entry.Value = value; }); _logger.LogSet(BuildTarget(nameof(Set)), fullKey, value !, _logSensitive); activity.SetEvent(CacheEvents.Set); }