public bool TryGet(TKey key, out TValue value)
        {
            var stopwatch = StopwatchStruct.StartNew();

            try
            {
                var found = _innerCache.TryGet(key, out value);

                OnTryGetCompletedSuccessfully(key, found, value, stopwatch.Elapsed);

                return(found);
            }
            catch (Exception ex)
            {
                OnTryGetException(key, stopwatch.Elapsed, ex, out var exceptionHandled);

                if (!exceptionHandled)
                {
                    throw;
                }

                value = default;
                return(false);
            }
        }
Esempio n. 2
0
        public ValueTask <(bool Success, TValue Value, CacheGetStats Stats)> TryGet(TKey key)
        {
            var flags = CacheGetFlags.LocalCache_Enabled | CacheGetFlags.DistributedCache_Enabled;

            if (_skipLocalCacheGetPredicate is null || !_skipLocalCacheGetPredicate(key))
            {
                flags |= CacheGetFlags.LocalCache_KeyRequested;

                if (_localCache.TryGet(key, out var value))
                {
                    flags |= CacheGetFlags.LocalCache_Hit;
                    return(new ValueTask <(bool, TValue, CacheGetStats)>((true, value, flags.ToStats())));
                }
            }
Esempio n. 3
0
        public bool TryGet(TKey key, out TValue value)
        {
            Interlocked.Increment(ref TryGetExecutionCount);

            if (_throwExceptionOnNextAction)
            {
                _throwExceptionOnNextAction = false;
                throw new Exception();
            }

            if (_innerCache.TryGet(key, out value))
            {
                Interlocked.Increment(ref HitsCount);
                return(true);
            }

            Interlocked.Increment(ref MissesCount);
            return(false);
        }