/// <summary>
        /// Ons the message.
        /// </summary>
        /// <param name="message">Message.</param>
        private void OnMessage(EasyCachingMessage message)
        {
            // each clients will recive the message, current client should ignore.
            if (!string.IsNullOrWhiteSpace(message.Id) && message.Id.Equals(_cacheId, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            //remove by prefix
            if (message.IsPrefix)
            {
                var prefix = message.CacheKeys.First();

                _localCache.RemoveByPrefix(prefix);

                if (_options.EnableLogging)
                {
                    _logger.LogTrace($"remove local cache that prefix is {prefix}");
                }

                return;
            }

            foreach (var item in message.CacheKeys)
            {
                _localCache.Remove(item);

                if (_options.EnableLogging)
                {
                    _logger.LogTrace($"remove local cache that cache key is {item}");
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Caches the delete action.
        /// </summary>
        /// <param name="channel">Channel.</param>
        /// <param name="value">Value.</param>
        private void CacheDeleteAction(RedisChannel channel, RedisValue value)
        {
            var message = _serializer.Deserialize <EasyCachingMessage>(value);

            //TODO : remove local cache
            _localCachingProvider.Remove(message.CacheKey);
        }
Exemple #3
0
        /// <summary>
        /// Remove the specified cacheKey.
        /// </summary>
        /// <returns>The remove.</returns>
        /// <param name="cacheKey">Cache key.</param>
        public void Remove(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            _localCachingProvider.Remove(cacheKey);
            _distributedCachingProvider.Remove(cacheKey);
        }
Exemple #4
0
        /// <summary>
        /// Invalidates all of the cache entries which are dependent on any of the specified root keys.
        /// </summary>
        /// <param name="cacheKey">Stores information of the computed key of the input LINQ query.</param>
        public void InvalidateCacheDependencies(EFCacheKey cacheKey)
        {
            _readerWriterLockProvider.TryWriteLocked(() =>
            {
                foreach (var rootCacheKey in cacheKey.CacheDependencies)
                {
                    if (string.IsNullOrWhiteSpace(rootCacheKey))
                    {
                        continue;
                    }

                    clearDependencyValues(rootCacheKey);
                    _easyCachingProvider.Remove(rootCacheKey);
                }
            });
        }
Exemple #5
0
        public string Get(string str)
        {
            var method = str.ToLower();

            switch (method)
            {
            case "get":
                var res = _provider.Get("demo", () => "456", TimeSpan.FromMinutes(1));
                return($"cached value : {res}");

            case "set":
                _provider.Set("demo", "123", TimeSpan.FromMinutes(1));
                return("seted");

            case "remove":
                _provider.Remove("demo");
                return("removed");

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

            default:
                return("default");
            }
        }
 public void DeleteUser(Guid userId, IEasyCachingProvider _easyCaching)
 {
     try
     {
         _easyCaching.Remove(userId.ToString());
     }
     catch
     { }
 }
Exemple #7
0
        public void Remove(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            _expirations.TryRemove(key, out TimeSpan _);

            _easyCachingProvider.Remove(key);
        }
Exemple #8
0
        /// <summary>
        /// Processes the evict.
        /// </summary>
        /// <param name="invocation">Invocation.</param>
        /// <param name="isBefore">If set to <c>true</c> is before.</param>
        private void ProcessEvict(IInvocation invocation, bool isBefore)
        {
            var serviceMethod = invocation.MethodInvocationTarget ?? invocation.Method;

            var attribute = serviceMethod.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(EasyCachingEvictAttribute)) as EasyCachingEvictAttribute;

            if (attribute != null && attribute.IsBefore == isBefore)
            {
                var cacheKey = _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix);

                _cacheProvider.Remove(cacheKey);
            }
        }
        public IActionResult DeletePerson(int id)
        {
            if (!_cachingProvider.Exists(id.ToString()))
            {
                return(BadRequest($"the person with id {id} does not exist ..."));
            }

            _cachingProvider.Remove(id.ToString());

            Debug.WriteLine($"person with id {id} deleted");

            return(Ok());
        }
        /// <summary>
        /// Remove the specified cacheKey.
        /// </summary>
        /// <param name="cacheKey">Cache key.</param>
        public void Remove(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            //distributed cache at first
            _distributedCache.Remove(cacheKey);
            _localCache.Remove(cacheKey);

            //send message to bus
            _bus.Publish(_options.TopicName, new EasyCachingMessage {
                Id = _cacheId, CacheKeys = new string[] { cacheKey }
            });
        }
Exemple #11
0
        public async Task <ActionResult <IEnumerable <string> > > Get()
        {
            _logger.LogInformation("正在调用接口 GET api/values ");

            var client = _httpClientFactory.CreateClient("cnblogs");
            var result = await client.GetStringAsync("xlgwr");

            //Remove
            _provider.Remove("demo");

            //Set
            _provider.Set("demo", result, TimeSpan.FromMinutes(1));

            return(new string[] { "value1", "value2", result });
        }
        public IActionResult Index()
        {
            var dic = new Dictionary <int, string>();

            dic.Add(1, "a");

            _cache.Set("Tjl", dic, TimeSpan.FromDays(100));

            var res = _cache.Get <Dictionary <int, string> >("Tjl");


            _cache.Remove("zaranet use easycaching");


            return(View());
        }
Exemple #13
0
        /// <summary>
        /// Consumers the received.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void OnConsumerReceived(object sender, BasicDeliverEventArgs e)
        {
            var message = _serializer.Deserialize <EasyCachingMessage>(e.Body);

            switch (message.NotifyType)
            {
            case NotifyType.Add:
                _localCachingProvider.Set(message.CacheKey, message.CacheValue, message.Expiration);
                break;

            case NotifyType.Update:
                _localCachingProvider.Refresh(message.CacheKey, message.CacheValue, message.Expiration);
                break;

            case NotifyType.Delete:
                _localCachingProvider.Remove(message.CacheKey);
                break;
            }
        }
        /// <summary>
        /// Subscribes the handle.
        /// </summary>
        /// <param name="channel">Channel.</param>
        /// <param name="value">Value.</param>
        private void SubscribeHandle(RedisChannel channel, RedisValue value)
        {
            var message = _serializer.Deserialize <EasyCachingMessage>(value);

            switch (message.NotifyType)
            {
            case NotifyType.Add:
                _localCachingProvider.Set(message.CacheKey, message.CacheValue, message.Expiration);
                break;

            case NotifyType.Update:
                _localCachingProvider.Refresh(message.CacheKey, message.CacheValue, message.Expiration);
                break;

            case NotifyType.Delete:
                _localCachingProvider.Remove(message.CacheKey);
                break;
            }
        }
        /// <summary>
        /// Remove the specified cacheKey.
        /// </summary>
        /// <param name="cacheKey">Cache key.</param>
        public void Remove(string cacheKey)
        {
            ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

            try
            {
                // distributed cache at first
                _distributedCache.Remove(cacheKey);
            }
            catch (Exception ex)
            {
                LogMessage($"remove cache key [{cacheKey}] error", ex);
            }

            _localCache.Remove(cacheKey);

            // send message to bus
            _busSyncWrap.Execute(() => _bus.Publish(_options.TopicName, new EasyCachingMessage {
                Id = _cacheId, CacheKeys = new string[] { cacheKey }
            }));
        }
        /// <summary>
        /// Processes the evict.
        /// </summary>
        /// <param name="invocation">Invocation.</param>
        /// <param name="isBefore">If set to <c>true</c> is before.</param>
        private void ProcessEvict(IInvocation invocation, bool isBefore)
        {
            var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget;

            if (serviceMethod.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(EasyCachingEvictAttribute)) is EasyCachingEvictAttribute attribute && attribute.IsBefore == isBefore)
            {
                if (attribute.IsAll)
                {
                    //If is all , clear all cached items which cachekey start with the prefix.
                    var cacheKeyPrefix = _keyGenerator.GetCacheKeyPrefix(serviceMethod, attribute.CacheKeyPrefix);

                    _cacheProvider.RemoveByPrefix(cacheKeyPrefix);
                }
                else
                {
                    //If not all , just remove the cached item by its cachekey.
                    var cacheKey = _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix);

                    _cacheProvider.Remove(cacheKey);
                }
            }
        }
Exemple #17
0
 public string Get(int type = 1)
 {
     if (type == 1)
     {
         _provider.Remove("demo");
         return("removed");
     }
     else if (type == 2)
     {
         _provider.Set("demo", "123", TimeSpan.FromMinutes(1));
         return("seted");
     }
     else if (type == 3)
     {
         var res = _provider.Get("demo", () => "456", TimeSpan.FromMinutes(1));
         return($"cached value : {res}");
     }
     else
     {
         return("error");
     }
 }
Exemple #18
0
        /// <summary>
        /// Processes the evict.
        /// </summary>
        /// <param name="invocation">Invocation.</param>
        /// <param name="isBefore">If set to <c>true</c> is before.</param>
        private void ProcessEvict(IInvocation invocation, bool isBefore)
        {
            var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget;

            if (GetMethodAttributes(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 cacheKeyPrefix = _keyGenerator.GetCacheKeyPrefix(serviceMethod, attribute.CacheKeyPrefix);

                        _cacheProvider.RemoveByPrefix(cacheKeyPrefix);
                    }
                    else
                    {
                        //If not all , just remove the cached item by its cachekey.
                        var cacheKey = _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix);

                        _cacheProvider.Remove(cacheKey);
                    }
                }
                catch (Exception ex)
                {
                    if (!attribute.IsHightAvailability)
                    {
                        throw;
                    }
                    else
                    {
                        _logger?.LogError(new EventId(), ex, $"Cache provider \"{_cacheProvider.Name}\" remove error.");
                    }
                }
            }
        }
 /// <summary>
 /// 清除缓存数据(同步)
 /// </summary>
 public void ClearCacheData()
 {
     _provider.Remove(_cacheKey);
 }
Exemple #20
0
 /// <summary>
 /// 移除缓存
 /// </summary>
 /// <param name="key">缓存键</param>
 public void Remove(string key)
 {
     _provider.Remove(key);
 }
        protected virtual void LPush_And_LPop_Should_Succeed()
        {
            var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}";

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

            Assert.Equal(2, res);

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

            Assert.Equal("p2", val);

            _baseProvider.Remove(cacheKey);
        }
Exemple #22
0
 public void Remove(string cacheKey)
 {
     _provider.Remove(cacheKey);
 }
        //public void CacheDelete(string id, T model)
        //{
        //    _provider.Remove(id);

        //}

        //public void CatcheDelete(string text, string id, T model)
        //{

        //}

        public void Delete(string id)
        {
            _provider.Remove(id);
        }
 private void SetItemToCache(string json, string key)
 {
     _provider.Remove(key);
     _provider.Set(key, json, TimeSpan.FromHours(24));
 }
Exemple #25
0
 public void Remove_Cached_Value_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey)
 {
     Assert.Throws <ArgumentNullException>(() => _provider.Remove(cacheKey));
 }
 public void Delete(string key)
 {
     _cachingProvider.Remove(key);
 }
        public async Task RemoveById(Guid recipeDetailsId)
        {
            var key = recipeDetailsId.ToDictionaryKey(nameof(RecipeDetails));

            _cachingProvider.Remove(key);
        }
Exemple #28
0
 /// <summary>
 /// Removes the specified cacheKey.
 /// </summary>
 /// <param name="cacheKey">Cache key.</param>
 public void Remove(string cacheKey)
 {
     _easyCachingProvider.Remove(cacheKey);
 }