Пример #1
0
        /// <summary>
        /// 设置缓存,数据类型为Hash
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="node"></param>
        /// <returns></returns>
        public async Task SetHashAsync <T>(CacheNode <T> node)
        {
            await Task.Run(() =>
            {
                var db    = RedisCacheConnection.CreateInstance().Database;
                var batch = db.CreateBatch();
                var maps  = ToMap(node.Data);

                if (node.CacheTime != default(TimeSpan))
                {
                    var currentTime = DateTime.Now;
                    maps.Add("_ExpiryTime_", currentTime.Add(node.CacheTime).ToString());
                }

                foreach (var map in maps)
                {
                    batch.HashSetAsync(node.Key, map.Key, map.Value);
                }

                batch.Execute();
            });
        }
Пример #2
0
        public async Task InvokeAsync(InvocationContext context)
        {
            Console.WriteLine("11");
            policies.TryGetValue(context.Method, out Policy policy);

            lock (policies)
            {
                if (policy == null)
                {
                    //创建一个空的Policy
                    policy = Policy.NoOpAsync();

                    //启用熔断
                    if (IsEnableBreaker)
                    {
                        policy = policy.WrapAsync(
                            Policy.Handle <Exception>()
                            .CircuitBreakerAsync(ExceptionsAllowedBeforeBreaking, TimeSpan.FromMilliseconds(MillisecondsOfBreak)));
                    }
                    //启用超时
                    if (TimeOutMilliseconds > 0)
                    {
                        policy = policy.WrapAsync(
                            Policy.TimeoutAsync(
                                () => TimeSpan.FromMilliseconds(TimeOutMilliseconds), Polly.Timeout.TimeoutStrategy.Pessimistic));
                    }
                    //启用重试
                    if (MaxRetryCount > 0)
                    {
                        policy = policy.WrapAsync(
                            Policy.Handle <Exception>()
                            .WaitAndRetryAsync(MaxRetryCount, i => TimeSpan.FromMilliseconds(RetryIntervalMilliseconds)));
                    }

                    //放入
                    policies.TryAdd(context.Method, policy);
                }
            }

            Context pollyCtx = new Context();

            pollyCtx["aspectContext"] = context;

            //启用缓存
            if (CacheTTLMilliseconds > 0)
            {
                //用类名+方法名+参数的下划线连接起来作为缓存key
                string cacheKey = "Cache_Key_" + context.Method.DeclaringType + "." + context.Method + string.Join("_", context.Arguments);

                RedisCache cache = new RedisCache();
                var        value = cache.Get(cacheKey);
                if (!string.IsNullOrEmpty(value))
                {
                    context.ReturnValue = value;
                }
                else
                {
                    //如果缓存中没有,则执行实际被拦截的方法
                    await policy.ExecuteAsync(ctx => _next(context), pollyCtx);

                    var tp   = new TimeSpan(0, 0, 0, 0, CacheTTLMilliseconds);
                    var node = new CacheNode <string> {
                        Key = cacheKey, Data = context.ReturnValue.ToObjString(), CacheTime = tp
                    };
                    cache.Set(node);
                }
            }
            else//如果没有启用缓存,就直接执行业务方法
            {
                await policy.ExecuteAsync(ctx => _next(context), pollyCtx);
            }
        }