protected virtual async Task TrySet_Value_And_Get_Cached_Value_Async_Should_Succeed()
        {
            var cacheKey    = $"{_nameSpace}{Guid.NewGuid().ToString()}";
            var cacheValue1 = "value1";
            var cacheValue2 = "value2";

            var first = await _provider.TrySetAsync(cacheKey, cacheValue1, _defaultTs);

            var second = await _provider.TrySetAsync(cacheKey, cacheValue2, _defaultTs);

            Assert.True(first);
            Assert.False(second);

            var val = _provider.Get <string>(cacheKey);

            Assert.True(val.HasValue);
            Assert.Equal(cacheValue1, val.Value);
        }
예제 #2
0
        /// <summary>
        /// Tries the set async.
        /// </summary>
        /// <returns>The set async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <param name="cacheValue">Cache value.</param>
        /// <param name="expiration">Expiration.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task <bool> TrySetAsync <T>(string cacheKey, T cacheValue, TimeSpan expiration)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            var flag = await _distributedCache.TrySetAsync(cacheKey, cacheValue, expiration);

            if (flag)
            {
                //When we TrySet succeed in distributed cache, we should Set this cache to local cache.
                await _localCache.SetAsync(cacheKey, cacheValue, expiration);
            }

            return(flag);
        }
예제 #3
0
        public async Task <CacheValue <T> > Get(string cacheKey, Func <int, Task <T> > valueResolver)
        {
            var fullCacheKey = CreateCacheKey(cacheKey);
            T   value;

            var cachedValue = await _cache.GetAsync <T>(fullCacheKey);

            value = cachedValue.Value;

            if (!cachedValue.HasValue)
            {
                value = await valueResolver(1);

                await _cache.TrySetAsync <T>(fullCacheKey, value, TimeSpan.FromMinutes(1));
            }

            return(new CacheValue <T>(value));
        }
        /// <summary>
        /// Tries the set async.
        /// </summary>
        /// <returns>The set async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        /// <param name="cacheValue">Cache value.</param>
        /// <param name="expiration">Expiration.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task <bool> TrySetAsync <T>(string cacheKey, T cacheValue, TimeSpan expiration)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            bool distributedError = false;
            bool flag             = false;

            try
            {
                flag = await _distributedCache.TrySetAsync(cacheKey, cacheValue, expiration);
            }
            catch (Exception ex)
            {
                distributedError = true;
                LogMessage($"tryset cache key [{cacheKey}] error", ex);
            }

            if (flag && !distributedError)
            {
                // When we TrySet succeed in distributed cache, we should Set this cache to local cache.
                // It's mainly to prevent the cache value was changed
                await _localCache.SetAsync(cacheKey, cacheValue, expiration);
            }

            // distributed cache occur error, have a try with local cache
            if (distributedError)
            {
                flag = await _localCache.TrySetAsync(cacheKey, cacheValue, expiration);
            }

            if (flag)
            {
                // Here should send message to bus due to cache was set successfully.
                await _busAsyncWrap.ExecuteAsync(async() => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage {
                    Id = _cacheId, CacheKeys = new string[] { cacheKey }
                }));
            }

            return(flag);
        }
 public Task <bool> TryAddAsync <T>(string key, T value, TimeSpan?expiration = null)
 {
     return(_provider.TrySetAsync(key, value, GetExpiration(expiration)));
 }
예제 #6
0
        /// <summary>
        /// Tries the set async.
        /// </summary>
        /// <typeparam name="T">Type of cache value</typeparam>
        /// <param name="cacheKey">Cache key</param>
        /// <param name="cacheValue">Cache value</param>
        /// <param name="expirationMinutes">Expiration in minutes</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns>Determines whether it is successful</returns>
        public Task <bool> TrySetAsync <T>(string cacheKey, T cacheValue, int?expirationMinutes = null, CancellationToken cancellationToken = default)
        {
            var timespan = TimeSpan.FromMinutes(expirationMinutes ?? _options.DefaultCacheMinutes);

            return(_easyCachingProvider.TrySetAsync(cacheKey, cacheValue, timespan));
        }