示例#1
0
    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);
    }
示例#2
0
    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);
    }
示例#3
0
    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);
示例#4
0
    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);
    }