Ejemplo n.º 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);
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
 }
Ejemplo n.º 11
0
 public Task <bool> DoesExist(string key)
 {
     return(_provider.ExistsAsync(key));
 }