Пример #1
0
 /// <inheritdoc />
 /// <summary>
 /// </summary>
 /// <param name="keyNamespace">缓存命名空间</param>
 /// <param name="keyPerfix">缓存子域</param>
 /// <param name="expires">过期时间(秒)</param>
 public StringCacheableAttribute(string keyNamespace, string keyPerfix, int expires)
 {
     KeyNamespace  = keyNamespace;
     Expires       = TimeSpan.FromSeconds(expires);
     KeyPerfix     = keyPerfix;
     CacheRepo     = new CacheRepo();
     _keyGenerator = new DefaultKeyGenerator();
 }
 /// <inheritdoc />
 /// <summary>
 /// </summary>
 /// <param name="keyNamespace">缓存命名空间</param>
 /// <param name="keyPerfix">缓存前缀</param>
 /// <param name="fieldIndex"></param>
 public StringCacheEvitAttribute(string keyNamespace, string[] keyPerfix, params int[] fieldIndex)
 {
     CacheRepo     = new CacheRepo();
     _keyGenerator = new DefaultKeyGenerator();
     KeyNamespace  = keyNamespace;
     KeysPerfix    = keyPerfix;
     FieldIndex    = fieldIndex;
 }
        /// <summary>
        /// 更新缓存
        /// </summary>
        /// <param name="arguments">拦截方法的参数</param>
        /// <param name="returnValue">拦截方法的返回值</param>
        /// <param name="returnType">拦截方法的返回类型</param>
        public void UpdateIntercept(object[] arguments, Type returnType, object returnValue)
        {
            var leftKey = _keyGenerator.GetKeyRegion(KeyNamespace,
                                                     string.IsNullOrEmpty(KeyPerfix) ? returnType.Name : KeyPerfix);
            var rightKey = _keyGenerator.GetRightKey(arguments, ItemArgsIndex);
            var cacheKey = _keyGenerator.GetCacheKey(leftKey, rightKey);

            CacheRepo.StringSet(cacheKey, returnValue, returnType, Expires);
        }
 /// <inheritdoc />
 /// <summary>
 /// </summary>
 /// <param name="keyNamespace">缓存命名缓存</param>
 /// <param name="expires">过期时间(秒)</param>
 /// <param name="argsIndex">拦截方法的参数索引</param>
 /// <param name="keyPerfix">缓存子域</param>
 public StringCachePutAttribute(string keyNamespace, string keyPerfix, int expires, params int[] argsIndex)
 {
     KeyNamespace  = keyNamespace;
     Expires       = TimeSpan.FromSeconds(expires);
     ItemArgsIndex = argsIndex;
     KeyPerfix     = keyPerfix;
     CacheRepo     = new CacheRepo();
     _keyGenerator = new DefaultKeyGenerator();
 }
Пример #5
0
        /// <summary>
        /// 加入缓存
        /// </summary>
        /// <param name="arguments">拦截方法的参数</param>
        /// <param name="returnValue">拦截方法的返回值</param>
        /// <param name="returnType">拦截方法的返回类型</param>
        public void StoreIntercept(object[] arguments, Type returnType, object returnValue)
        {
            var filedKey  = _keyGenerator.GetRightKey(arguments, FieldIndex);
            var regionKey = _keyGenerator.GetKeyRegion(KeyNamespace, string.IsNullOrEmpty(KeyPerfix) ? returnType.Name : KeyPerfix);
            var itemKey   = KeyIndex != null && KeyIndex.Length > 0 ? _keyGenerator.GetRightKey(arguments, KeyIndex) : "";
            var cacheKey  = _keyGenerator.GetCacheKey(regionKey, itemKey);

            CacheRepo.HashSet(cacheKey, filedKey, returnValue, returnType, Expires);
        }
 /// <summary>
 /// 移除缓存
 /// </summary>
 /// <param name="arguments">拦截方法的参数</param>
 /// <param name="returnType">拦截方法的返回类型</param>
 /// <param name="returnValue">拦截方法的返回值</param>
 public void RemoveIntercept(object[] arguments, Type returnType, object returnValue)
 {
     foreach (var keyPerfix in KeysPerfix)
     {
         var regionKey = _keyGenerator.GetKeyRegion(KeyNamespace,
                                                    string.IsNullOrEmpty(keyPerfix) ? returnType.Name : keyPerfix);
         var itemKeys = new List <string>();
         if (KeyIndex != null && KeyIndex.Length > 0)
         {
             itemKeys.Add(_keyGenerator.GetRightKey(arguments, KeyIndex));
             foreach (var key in KeyIndex)
             {
                 itemKeys.Add(_keyGenerator.GetRightKey(arguments, key));
             }
         }
         var fieldKeys = new List <string>();
         if (FieldIndex != null && FieldIndex.Length > 0)
         {
             fieldKeys.Add(_keyGenerator.GetRightKey(arguments, FieldIndex));
             foreach (var field in FieldIndex)
             {
                 fieldKeys.Add(_keyGenerator.GetRightKey(arguments, field));
             }
         }
         var cacheKeys = new List <string>();
         if (itemKeys.Count > 0)
         {
             foreach (var itemKey in itemKeys)
             {
                 cacheKeys.Add(_keyGenerator.GetCacheKey(regionKey, itemKey));
             }
         }
         else
         {
             cacheKeys.Add(regionKey);
         }
         cacheKeys = cacheKeys.Distinct().ToList();
         if (cacheKeys.Count > 0)
         {
             foreach (var cacheKey in cacheKeys)
             {
                 if (fieldKeys.Count > 0)
                 {
                     foreach (var filedKey in fieldKeys)
                     {
                         CacheRepo.HashDelete(cacheKey, filedKey);
                     }
                 }
                 else
                 {
                     CacheRepo.KeyDelete(cacheKey);
                 }
             }
         }
     }
 }
Пример #7
0
 /// <inheritdoc />
 /// <summary>
 /// </summary>
 /// <param name="keyNamespace">缓存命名空间</param>
 /// <param name="keyPerfix">缓存子域</param>
 /// <param name="expires">过期时间(秒)</param>
 /// <param name="keyIndex">拦截方法参数索引(做为hashKey的一部分)</param>
 /// <param name="fieldIndex">拦截方法参数索引(做为filedKey的一部分)</param>
 public HashCachePostAttribute(string keyNamespace, string keyPerfix, int expires, int[] keyIndex, params int[] fieldIndex)
 {
     _keyGenerator = new DefaultKeyGenerator();
     CacheRepo     = new CacheRepo();
     KeyNamespace  = keyNamespace;
     Expires       = TimeSpan.FromSeconds(expires);
     FieldIndex    = fieldIndex;
     KeyIndex      = keyIndex;
     KeyPerfix     = keyPerfix;
 }
Пример #8
0
        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="arguments">拦截方法的参数</param>
        /// <param name="returnType">拦截方法的返回类型</param>
        /// <returns></returns>
        public object LoadIntercept(object[] arguments, Type returnType)
        {
            var rightKey = _keyGenerator.GetRightKey(arguments);
            var leftKey  = _keyGenerator.GetKeyRegion(KeyNamespace,
                                                      string.IsNullOrEmpty(KeyPerfix) ? returnType.Name : KeyPerfix);
            var cacheKey = _keyGenerator.GetCacheKey(leftKey, rightKey);
            var cache    = CacheRepo.StringGet(cacheKey, returnType);

            return(cache);
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="arguments">拦截方法的参数</param>
        /// <param name="returnType">拦截方法的返回类型</param>
        /// <param name="returnValue">拦截方法的返回值</param>
        public void RemoveIntercept(object[] arguments, Type returnType, object returnValue)
        {
            foreach (var keyPerfix in KeysPerfix)
            {
                var leftKey = _keyGenerator.GetKeyRegion(KeyNamespace,
                                                         string.IsNullOrEmpty(keyPerfix) ? returnType.Name : keyPerfix);

                var rightKeys = new List <string>();
                if (FieldIndex != null && FieldIndex.Length > 0)
                {
                    rightKeys.Add(_keyGenerator.GetRightKey(arguments, FieldIndex));
                    foreach (var field in FieldIndex)
                    {
                        rightKeys.Add(_keyGenerator.GetRightKey(arguments, field));
                    }
                }
                var cacheKeys = new List <string>();
                if (rightKeys.Count > 0)
                {
                    foreach (var rightKey in rightKeys)
                    {
                        cacheKeys.Add(_keyGenerator.GetCacheKey(leftKey, rightKey));
                    }
                    cacheKeys = cacheKeys.Distinct().ToList();
                    if (cacheKeys.Count > 0)
                    {
                        foreach (var cacheKey in cacheKeys)
                        {
                            CacheRepo.KeyDelete(cacheKey);
                        }
                    }
                }
                else
                {
                    cacheKeys.Add(leftKey + ":*");
                    cacheKeys = cacheKeys.Distinct().ToList();
                    if (cacheKeys.Count > 0)
                    {
                        foreach (var cacheKey in cacheKeys)
                        {
                            CacheRepo.KeyDeletePattern(cacheKey);
                        }
                    }
                }
            }
        }