protected virtual void GetExpiration_Should_Succeed()
        {
            var cacheKey    = $"{_nameSpace}{Guid.NewGuid().ToString()}";
            var cacheValue1 = "value1";

            _provider.Set(cacheKey, cacheValue1, _defaultTs);

            var ts = _provider.GetExpiration(cacheKey);

            Assert.InRange(ts, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30 + 120));
        }
Esempio n. 2
0
        public TimeSpan GetExpiration(string cacheKey)
        {
            var ts = _localCache.GetExpiration(cacheKey);

            if (ts > TimeSpan.Zero)
            {
                return(ts);
            }

            try
            {
                ts = _distributedCache.GetExpiration(cacheKey);
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "Error getting expiration for cache key = '{0}'.", cacheKey);
                return(TimeSpan.Zero);
            }

            return(ts);
        }
        public void GetExpiration_LocalExpirationEqualsZero_ReturnsDistributedExpiration()
        {
            var(hybridProvider, fakeLocalProvider, fakeDistributedProvider) = CreateFakeHybridProvider();
            var localExpiration      = TimeSpan.Zero;
            var distributeExpiration = TimeSpan.FromMinutes(5);
            var cacheKey             = GetUniqueCacheKey();

            A.CallTo(() => fakeLocalProvider.GetExpiration(cacheKey)).Returns(localExpiration);
            A.CallTo(() => fakeDistributedProvider.GetExpiration(cacheKey)).Returns(distributeExpiration);

            var ts = hybridProvider.GetExpiration(cacheKey);

            Assert.Equal(ts, distributeExpiration);
        }
        /// <summary>
        /// Get the specified cacheKey.
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public CacheValue <T> Get <T>(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            var cacheValue = _localCache.Get <T>(cacheKey);

            if (cacheValue.HasValue)
            {
                return(cacheValue);
            }

            LogMessage($"local cache can not get the value of {cacheKey}");

            // Circuit Breaker may be more better
            try
            {
                cacheValue = _distributedCache.Get <T>(cacheKey);
            }
            catch (Exception ex)
            {
                LogMessage($"distributed cache get error, [{cacheKey}]", ex);
            }

            if (cacheValue.HasValue)
            {
                TimeSpan ts = TimeSpan.Zero;

                try
                {
                    ts = _distributedCache.GetExpiration(cacheKey);
                }
                catch
                {
                }

                if (ts <= TimeSpan.Zero)
                {
                    ts = TimeSpan.FromSeconds(_options.DefaultExpirationForTtlFailed);
                }

                _localCache.Set(cacheKey, cacheValue.Value, ts);

                return(cacheValue);
            }

            LogMessage($"distributed cache can not get the value of {cacheKey}");

            return(CacheValue <T> .NoValue);
        }
        /// <summary>
        /// Get the specified cacheKey.
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public CacheValue <T> Get <T>(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            var cacheValue = _localCache.Get <T>(cacheKey);

            if (cacheValue.HasValue)
            {
                return(cacheValue);
            }

            if (_options.EnableLogging)
            {
                _logger.LogTrace($"local cache can not get the value of {cacheKey}");
            }

            cacheValue = _distributedCache.Get <T>(cacheKey);

            if (cacheValue.HasValue)
            {
                TimeSpan ts = TimeSpan.Zero;

                try
                {
                    ts = _distributedCache.GetExpiration(cacheKey);
                }
                catch
                {
                }

                if (ts <= TimeSpan.Zero)
                {
                    ts = TimeSpan.FromSeconds(_options.DefaultExpirationForTtlFailed);
                }

                _localCache.Set(cacheKey, cacheValue.Value, ts);

                return(cacheValue);
            }

            if (_options.EnableLogging)
            {
                _logger.LogTrace($"distributed cache can not get the value of {cacheKey}");
            }

            return(CacheValue <T> .NoValue);
        }
        private TimeSpan GetExpiration(string cacheKey)
        {
            TimeSpan ts = TimeSpan.Zero;

            try
            {
                ts = _distributedCache.GetExpiration(cacheKey);
            }
            catch
            {
            }

            if (ts <= TimeSpan.Zero)
            {
                ts = TimeSpan.FromSeconds(_options.DefaultExpirationForTtlFailed);
            }

            return(ts);
        }
Esempio n. 7
0
 /// <summary>
 /// Gets the exporation of specify cachekey async.
 /// </summary>
 /// <param name="cacheKey">Cache key.</param>
 /// <returns>Expiration in TimeSpan</returns>
 public TimeSpan GetExpiration(string cacheKey)
 {
     return(_easyCachingProvider.GetExpiration(cacheKey));
 }