Пример #1
0
        public async Task Invoke(HttpContext context)
        {
            var cacheKey = context.Request.GenerateCacheKeyFromRequest();

            if (!await _easyCachingProvider.ExistsAsync(cacheKey))
            {
                await using var swapStream = new MemoryStream();
                var originalResponseBody = context.Response.Body;
                context.Response.Body = swapStream;
                await _next(context);

                swapStream.Seek(0, SeekOrigin.Begin);
                string responseBody = await new StreamReader(swapStream).ReadToEndAsync();
                swapStream.Seek(0, SeekOrigin.Begin);
                context.Response.Body = originalResponseBody;

                await _easyCachingProvider.SetAsync(cacheKey, responseBody, TimeSpan.FromMinutes(1));

                await context.Response.WriteAsync(responseBody);
            }
            else
            {
                var cacheValue = await _easyCachingProvider.GetAsync <string>(cacheKey);

                await context.Response.WriteAsync(cacheValue.Value);
            }

            await this._next(context);
        }
Пример #2
0
        public async Task <T> GetAsync <T>(string key, Func <Task <T> > acquire, int?cacheTime = null)
        {
            if (await _provider.ExistsAsync(key))
            {
                return((await _provider.GetAsync <T>(key)).Value);
            }

            if ((cacheTime ?? CachingDefaultSettings.CacheTime) > 0)
            {
                await _provider.SetAsync(key, acquire, TimeSpan.FromMinutes(cacheTime ?? CachingDefaultSettings.CacheTime));
            }

            return(await acquire());
        }
Пример #3
0
        /// <summary>
        /// Existses the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task <bool> ExistsAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            var flag = false;

            flag = await _localCachingProvider.ExistsAsync(cacheKey);

            if (!flag)
            {
                flag = await _distributedCachingProvider.ExistsAsync(cacheKey);
            }

            return(flag);
        }
        protected virtual async Task SRemAsync_Without_Values_Should_Succeed()
        {
            var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}";

            var res = await _provider.SAddAsync(cacheKey, new List <string> {
                "s1", "s2"
            });

            var len = await _provider.SRemAsync <string>(cacheKey);

            Assert.Equal(1, len);

            var flag = await _baseProvider.ExistsAsync(cacheKey);

            Assert.False(flag);

            await _baseProvider.RemoveAsync(cacheKey);
        }
Пример #5
0
        /// <summary>
        /// Existses the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task <bool> ExistsAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            bool flag;

            try
            {
                flag = await _distributedCache.ExistsAsync(cacheKey);

                return(flag);
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "Check cache key [{0}] exists error", cacheKey);
            }

            flag = await _localCache.ExistsAsync(cacheKey);

            return(flag);
        }
        /// <summary>
        /// Existses the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task <bool> ExistsAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            bool flag;

            // Circuit Breaker may be more better
            try
            {
                flag = await _distributedCache.ExistsAsync(cacheKey);

                return(flag);
            }
            catch (Exception ex)
            {
                LogMessage($"Check cache key [{cacheKey}] exists error", ex);
            }

            flag = await _localCache.ExistsAsync(cacheKey);

            return(flag);
        }
Пример #7
0
 /// <summary>
 /// Gets availability of the cached value with specified key
 /// </summary>
 public async Task <bool> IsSetAsync(string key)
 {
     return(await _provider.ExistsAsync(key));
 }
Пример #8
0
 public async Task Exists_Cached_Value_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey)
 {
     await Assert.ThrowsAsync <ArgumentNullException>(async() => await _provider.ExistsAsync(cacheKey));
 }
Пример #9
0
        /// <summary>
        /// Existses the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task <bool> ExistsAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            return(await _distributedCache.ExistsAsync(cacheKey));
        }
Пример #10
0
 /// <summary>
 /// Whether the cached value contains the specified cacheKey async.
 /// </summary>
 /// <param name="cacheKey">Cache key.</param>
 /// <param name="cancellationToken">cancellationToken</param>
 /// <returns>The exists.</returns>
 public Task <bool> ExistsAsync(string cacheKey, CancellationToken cancellationToken = default)
 {
     return(_easyCachingProvider.ExistsAsync(cacheKey));
 }
Пример #11
0
 public Task <bool> DoesExist(string key)
 {
     return(_provider.ExistsAsync(key));
 }