コード例 #1
0
 private void RedisUnLock(string name = "")
 {
     var tmp = RedisCacheHelper.Del(name);
 }
コード例 #2
0
        public void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;

            //对当前方法的特性验证
            //如果需要验证
            if (method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(RedisAttribute)) is RedisAttribute)
            {
                var attributes = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(RedisAttribute));

                var attributesType    = ((RedisAttribute)attributes).Type;
                var attributesTimeout = ((RedisAttribute)attributes).Timeout;
                var attributesKey     = GetRedisKey(invocation);
                var attributesData    = ((RedisAttribute)attributes).Data;

                switch (attributesType)
                {
                case RedisAttributeType.Query:
                    var ret = RedisCacheHelper.Get <object>(attributesKey);
                    if (ret != null)
                    {
                        invocation.ReturnValue = ret;
                    }
                    else
                    {
                        invocation.Proceed();
                        if (invocation.ReturnValue != null)
                        {
                            RedisCacheHelper.Set(attributesKey, invocation.ReturnValue, attributesTimeout);
                        }
                    }
                    break;

                case RedisAttributeType.Insert:
                    invocation.Proceed();
                    if (invocation.ReturnValue != null && invocation.ReturnValue.ObjectToBool() == true)
                    {
                        var data = dic[attributesData];
                        RedisCacheHelper.Set(attributesKey, data, attributesTimeout);
                    }
                    break;

                case RedisAttributeType.Update:
                    invocation.Proceed();
                    if (invocation.ReturnValue != null && invocation.ReturnValue.ObjectToBool() == true)
                    {
                        var data = dic[attributesData];
                        RedisCacheHelper.Set(attributesKey, data, attributesTimeout);
                    }
                    break;

                case RedisAttributeType.Delete:
                    invocation.Proceed();
                    if (invocation.ReturnValue != null && invocation.ReturnValue.ObjectToBool() == true)
                    {
                        RedisCacheHelper.Del(attributesKey);
                    }
                    break;

                case RedisAttributeType.Refresh:
                    break;
                }
            }
            else
            {
                invocation.Proceed();//直接执行被拦截方法
            }
        }