Exemplo n.º 1
0
        public async Task <MessageModel <Users> > Get()
        {
            //if(id<=0)
            //    return new MessageModel<Users>() { Msg = "信息有误,请稍后再试!" };
            var loginId = await _redis.GetValue("LoginId");

            var user = _context.Users.AsQueryable().Where(u => u.LoginsId == loginId).FirstOrDefault();
            await _redis.Set("Users", user, TimeSpan.FromMinutes(30));

            user = await _redis.Get <Users>("Users");

            return(new MessageModel <Users>()
            {
                Msg = "成功!", Success = true, Response = user
            });
        }
Exemplo n.º 2
0
        public async Task <object> RedisChche()
        {
            var    data = new MessageModel <object>();
            var    list = _cache.Exist("list");
            object c    = new();

            if (list.Result == true)
            {
                c = _cache.GetValue("list").Result;
            }
            else
            {
                c = await tasksQzServices.Query();

                TimeSpan c1 = DateTime.Now.AddDays(60) - DateTime.Now;
                c = _cache.Set("list", c, c1);
            }
            return(c);
        }
Exemplo n.º 3
0
        //Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义
        public override void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;
            //对当前方法的特性验证
            var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute;

            if (qCachingAttribute != null)
            {
                //获取自定义缓存键,这个和Memory内存缓存是一样的,不细说
                var cacheKey = CustomCacheKey(invocation);
                //核心1:注意这里和之前不同,是获取的string值,之前是object
                var cacheValue = _cache.GetValue(cacheKey);
                if (cacheValue != null)
                {
                    //将当前获取到的缓存值,赋值给当前执行方法
                    var type        = invocation.Method.ReturnType;
                    var resultTypes = type.GenericTypeArguments;
                    if (type.FullName == "System.Void")
                    {
                        return;
                    }
                    object response;
                    if (type != null && typeof(Task).IsAssignableFrom(type))
                    {
                        //核心2:返回异步对象Task<T>
                        if (resultTypes.Count() > 0)
                        {
                            var resultType = resultTypes.FirstOrDefault();
                            // 核心3,直接序列化成 dynamic 类型,之前我一直纠结特定的实体
                            dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, resultType);
                            response = Task.FromResult(temp);
                        }
                        else
                        {
                            //Task 无返回方法 指定时间内不允许重新运行
                            response = Task.CompletedTask;
                        }
                    }
                    else
                    {
                        object value = _cache.Get <object>(cacheKey);
                        // 核心4,要进行 ChangeType
                        response = System.Convert.ChangeType(value, type);
                    }

                    invocation.ReturnValue = response;
                    return;
                }
                //去执行当前的方法
                invocation.Proceed();

                //存入缓存
                if (!string.IsNullOrWhiteSpace(cacheKey))
                {
                    object response;

                    //Type type = invocation.ReturnValue?.GetType();
                    var type = invocation.Method.ReturnType;
                    if (type != null && typeof(Task).IsAssignableFrom(type))
                    {
                        var resultProperty = type.GetProperty("Result");
                        response = resultProperty?.GetValue(invocation.ReturnValue);
                    }
                    else
                    {
                        response = invocation.ReturnValue;
                    }
                    if (response == null)
                    {
                        response = string.Empty;
                    }
                    // 核心5:将获取到指定的response 和特性的缓存时间,进行set操作
                    _cache.Set(cacheKey, response, TimeSpan.FromSeconds(qCachingAttribute.AbsoluteExpiration));
                }
            }
            else
            {
                invocation.Proceed();//直接执行被拦截方法
            }
        }
Exemplo n.º 4
0
        //Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义
        //代码已经合并 ,学习pr流程
        public override void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;

            if (method.ReturnType == typeof(void) || method.ReturnType == typeof(Task))
            {
                invocation.Proceed();
                return;
            }
            //对当前方法的特性验证
            var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute;

            if (qCachingAttribute != null)
            {
                //获取自定义缓存键
                var cacheKey = CustomCacheKey(invocation);
                //注意是 string 类型,方法GetValue
                var cacheValue = _cache.GetValue(cacheKey).Result;
                if (cacheValue != null)
                {
                    //将当前获取到的缓存值,赋值给当前执行方法
                    Type returnType;
                    if (typeof(Task).IsAssignableFrom(method.ReturnType))
                    {
                        returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
                    }
                    else
                    {
                        returnType = method.ReturnType;
                    }

                    dynamic _result = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, returnType);
                    invocation.ReturnValue = (typeof(Task).IsAssignableFrom(method.ReturnType)) ? Task.FromResult(_result) : _result;
                    return;
                }
                //去执行当前的方法
                invocation.Proceed();

                //存入缓存
                if (!string.IsNullOrWhiteSpace(cacheKey))
                {
                    object response;

                    //Type type = invocation.ReturnValue?.GetType();
                    var type = invocation.Method.ReturnType;
                    if (typeof(Task).IsAssignableFrom(type))
                    {
                        var resultProperty = type.GetProperty("Result");
                        response = resultProperty.GetValue(invocation.ReturnValue);
                    }
                    else
                    {
                        response = invocation.ReturnValue;
                    }
                    if (response == null)
                    {
                        response = string.Empty;
                    }

                    _cache.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration)).Wait();
                }
            }
            else
            {
                invocation.Proceed();//直接执行被拦截方法
            }
        }