/// <summary>
        /// Gets the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task <CacheValue <T> > GetAsync <T>(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

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

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

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

            try
            {
                cacheValue = await _distributedCache.GetAsync <T>(cacheKey);
            }
            catch (Exception ex)
            {
                LogMessage($"distributed cache get error, [{cacheKey}]", ex);
            }

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

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

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

                await _localCache.SetAsync(cacheKey, cacheValue.Value, ts);

                return(cacheValue);
            }

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

            return(CacheValue <T> .NoValue);
        }
        protected virtual async Task GetExpiration_Async_Should_Succeed()
        {
            var cacheKey    = $"{_nameSpace}{Guid.NewGuid().ToString()}";
            var cacheValue1 = "value1";

            await _provider.SetAsync(cacheKey, cacheValue1, _defaultTs);

            var ts = await _provider.GetExpirationAsync(cacheKey);

            Assert.InRange(ts, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30 + 120));
        }
Example #3
0
        public async Task <TimeSpan> GetExpirationAsync(string cacheKey)
        {
            var ts = await _localCache.GetExpirationAsync(cacheKey);

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

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

            return(ts);
        }
        private async Task <TimeSpan> GetExpirationAsync(string cacheKey)
        {
            TimeSpan ts = TimeSpan.Zero;

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

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

            return(ts);
        }
Example #5
0
 /// <summary>
 /// Gets the exporation of specify cachekey async.
 /// </summary>
 /// <param name="cacheKey">Cache key.</param>
 /// <param name="cancellationToken">cancellationToken</param>
 /// <returns>Expiration in TimeSpan</returns>
 public Task <TimeSpan> GetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default)
 {
     return(_easyCachingProvider.GetExpirationAsync(cacheKey));
 }
        public async Task GetExpirationAsync_LocalExpirationEqualsZero_ReturnsDistributedExpiration()
        {
            var(hybridProvider, fakeLocalProvider, fakeDistributedProvider) = CreateFakeHybridProvider();
            var localExpiration      = TimeSpan.Zero;
            var distributeExpiration = TimeSpan.FromMinutes(5);
            var cacheKey             = GetUniqueCacheKey();

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

            var ts = await hybridProvider.GetExpirationAsync(cacheKey);

            Assert.Equal(ts, distributeExpiration);
        }