예제 #1
0
        public async Task <string> GetAsync(int type = 1)
        {
            if (type == 1)
            {
                await _provider.RemoveAsync("demo");

                return("removed");
            }
            else if (type == 2)
            {
                await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));

                return("seted");
            }
            else if (type == 3)
            {
                var res = await _provider.GetAsync("demo", async() => await Task.FromResult("456"), TimeSpan.FromMinutes(1));

                return($"cached value : {res}");
            }
            else
            {
                return("error");
            }
        }
예제 #2
0
        public async Task <string> GetAsync(string str)
        {
            var method = str.ToLower();

            switch (method)
            {
            case "get":
                var res = await _provider.GetAsync("demo", async() => await Task.FromResult("456"), TimeSpan.FromMinutes(1));

                return($"cached value : {res}");

            case "set":
                await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));

                return("seted");

            case "remove":
                await _provider.RemoveAsync("demo");

                return("removed");

            case "getcount":
                var count = _provider.GetCount();
                return($"{count}");

            default:
                return("default");
            }
        }
예제 #3
0
        /// <summary>
        /// Processes the evict async.
        /// </summary>
        /// <returns>The evict async.</returns>
        /// <param name="context">Context.</param>
        /// <param name="isBefore">If set to <c>true</c> is before.</param>
        private async Task ProcessEvictAsync(AspectContext context, bool isBefore)
        {
            if (GetMethodAttributes(context.ServiceMethod).FirstOrDefault(x => x.GetType() == typeof(EasyCachingEvictAttribute)) is EasyCachingEvictAttribute attribute && attribute.IsBefore == isBefore)
            {
                try
                {
                    if (attribute.IsAll)
                    {
                        //If is all , clear all cached items which cachekey start with the prefix.
                        var cachePrefix = KeyGenerator.GetCacheKeyPrefix(context.ServiceMethod, attribute.CacheKeyPrefix);

                        await _cacheProvider.RemoveByPrefixAsync(cachePrefix);
                    }
                    else
                    {
                        //If not all , just remove the cached item by its cachekey.
                        var cacheKey = KeyGenerator.GetCacheKey(context.ServiceMethod, context.Parameters, attribute.CacheKeyPrefix);

                        await _cacheProvider.RemoveAsync(cacheKey);
                    }
                }
                catch (Exception ex)
                {
                    if (!attribute.IsHightAvailability)
                    {
                        throw;
                    }
                    else
                    {
                        Logger?.LogError(new EventId(), ex, $"Cache provider \"{_cacheProvider.Name}\" remove error.");
                    }
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Removes the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task RemoveAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            await _localCachingProvider.RemoveAsync(cacheKey);

            await _distributedCachingProvider.RemoveAsync(cacheKey);
        }
        public async void RemovePresignedUrlCache(string bucketName, string objectName)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (string.IsNullOrEmpty(objectName))
            {
                throw new ArgumentNullException(nameof(objectName));
            }
            if (Options.IsEnableCache)
            {
                string key = Encrypt.MD5($"{bucketName}_{objectName}_{PresignedObjectType.Put.ToString().ToUpper()}");
                await _cache.RemoveAsync(key);

                key = Encrypt.MD5($"{bucketName}_{objectName}_{PresignedObjectType.Get.ToString().ToUpper()}");
                await _cache.RemoveAsync(key);
            }
        }
예제 #6
0
        public Task RemoveAsync(string key, CancellationToken token = default)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            _expirations.TryRemove(key, out TimeSpan _);

            return(_easyCachingProvider.RemoveAsync(key));
        }
예제 #7
0
        /// <summary>
        /// Removes the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task RemoveAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            //distributed cache at first
            await _distributedCache.RemoveAsync(cacheKey);

            await _localCache.RemoveAsync(cacheKey);

            //send message to bus
            await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } });
        }
예제 #8
0
        /// <summary>
        /// Removes the specified cacheKey async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public async Task RemoveAsync(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            try
            {
                // distributed cache at first
                await _distributedCache.RemoveAsync(cacheKey);
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "remove cache key [{0}] error", cacheKey);
            }

            await _localCache.RemoveAsync(cacheKey);

            // send message to bus
            await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } });
        }
예제 #9
0
 /// <summary>
 /// 清除缓存数据(异步)
 /// </summary>
 public async Task ClearCacheDataAsync()
 {
     await _provider.RemoveAsync(_cacheKey);
 }
        protected virtual async Task LPushAsync_And_LPopAsync_Should_Succeed()
        {
            var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}";

            var res = await _provider.LPushAsync(cacheKey, new List <string> {
                "p1", "p2"
            });

            Assert.Equal(2, res);

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

            Assert.Equal("p2", val);

            await _baseProvider.RemoveAsync(cacheKey);
        }
예제 #11
0
        public async Task Update <T>(string key, T data, TimeSpan?expiration = null)
        {
            await _provider.RemoveAsync(key);

            await _provider.SetAsync(key, data, expiration ?? TimeSpan.FromMinutes(10));
        }
예제 #12
0
 /// <summary>
 /// Removes cache with specified key
 /// </summary>
 public async Task RemoveAsync(string key)
 {
     await _provider.RemoveAsync(key);
 }
예제 #13
0
 public async Task Remove_Cached_Value_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey)
 {
     await Assert.ThrowsAsync <ArgumentNullException>(async() => await _provider.RemoveAsync(cacheKey));
 }
예제 #14
0
 public async Task ClearCacheAsync() => await _cache.RemoveAsync(Constants.RedisKey.Settings);
예제 #15
0
 public Task Remove(string cacheKey) => _provider.RemoveAsync(cacheKey);
예제 #16
0
        /// <summary>
        /// 刷新 AccessToken。清除缓存,重新获取
        /// </summary>
        /// <returns></returns>
        public async Task <string> RefreshAccessToken()
        {
            await _provider.RemoveAsync(CacheKey);

            return(await GetAccessToken());
        }
예제 #17
0
 public Task RemoveAsync(string key)
 {
     return(_provider.RemoveAsync(key));
 }
예제 #18
0
 /// <summary>
 /// Removes the specified cacheKey async.
 /// </summary>
 /// <param name="cacheKey">Cache key.</param>
 /// <param name="cancellationToken">cancellationToken</param>
 /// <returns>Task</returns>
 public Task RemoveAsync(string cacheKey, CancellationToken cancellationToken = default)
 {
     return(_easyCachingProvider.RemoveAsync(cacheKey));
 }
예제 #19
0
        public async Task <ApiResult <bool> > DeleteCart(string key)
        {
            await cachingProvider.RemoveAsync(key);

            return(new ApiSuccessResult <bool>());
        }
예제 #20
0
 public async Task Clear(string cacheKey)
 {
     await _cache.RemoveAsync(CreateCacheKey(cacheKey));
 }
        public async Task <bool> DeleteBasketAsync(string id)
        {
            await easyCachingProvider.RemoveAsync("Basket_" + id);

            return(true);
        }