/// <summary> /// 根据指定的<see cref="CachingAttribute"/>以及<see cref="IMethodInvocation"/>实例, /// 获取与某一特定参数值相关的键名。 /// </summary> /// <param name="cachingAttribute"><see cref="CachingAttribute"/>实例。</param> /// <param name="input"><see cref="IMethodInvocation"/>实例。</param> /// <returns>与某一特定参数值相关的键名。</returns> private string GetValueKey(CachingAttribute cachingAttribute, IMethodInvocation input) { switch (cachingAttribute.Method) { // 如果是Remove,则不存在特定值键名,所有的以该方法名称相关的缓存都需要清除 case CachingMethod.Remove: return(null); // 如果是Get或者Put,则需要产生一个针对特定参数值的键名 case CachingMethod.Get: case CachingMethod.Put: if (input.Arguments != null && input.Arguments.Count > 0) { var sb = new StringBuilder(); for (int i = 0; i < input.Arguments.Count; i++) { sb.Append(input.Arguments[i].ToString()); if (i != input.Arguments.Count - 1) { sb.Append("_"); } } return(sb.ToString()); } else { return("NULL"); } default: throw new InvalidOperationException("无效的缓存方式。"); } }
public CallDescriptor(ControllerDescriptor controller, MethodInfo method) { Controller = controller; Descriptor = MethodDescriptorAttribute.GetDescriptor(method); CacheHeader = CachingAttribute.GetAttribute(method); TokenRequirements = RequiresTokenAttribute.GetAttribute(method); Info = method; Parameters = Info.GetParameters().Select(x => new ParameterDescriptor(x)).ToList(); JsonSerialization = JsonSerializerAttribute.GetAttribute(method); Attributes = Info.GetCustomAttributes().ToDictionary(x => x.GetType(), y => y); }
/// <summary> /// 根据指定的<see cref="CachingAttribute" />以及<see cref="IInvocation" />实例, /// 获取与某一特定参数值相关的键名。 /// </summary> /// <param name="cachingAttribute"><see cref="CachingAttribute" />实例。</param> /// <param name="invocation"><see cref="IInvocation" />实例。</param> /// <returns>与某一特定参数值相关的键名。</returns> private string GetValueKey(CachingAttribute cachingAttribute, IInvocation invocation) { switch (cachingAttribute.Method) { // 如果是Remove,则不存在特定值键名,所有的以该方法名称相关的缓存都需要清除 case CachingMethod.Remove: return(null); // 如果是Get或者Put,则需要产生一个针对特定参数值的键名 case CachingMethod.Get: case CachingMethod.Put: if ((invocation.Arguments != null) && invocation.Arguments.Any()) { var args = new List <string> { invocation.Method.Name }; foreach (var arg in invocation.Arguments) { if (arg != null) { var ar = arg.ToString(); if (arg.GetType().BaseType.Name == "LambdaExpression") { ar = new ExpressionTranslator().Translate(arg as Expression); } args.Add(ar); } } return(string.Join("_", args)); } return(invocation.Method.Name); default: throw new InvalidOperationException("无效的缓存方式。"); } }
public void Setup() { cachingAttribute = new CachingAttribute(); unityContainer = new UnityContainer(); }
private void CacheIntercept(IInvocation invocation, CachingAttribute cacheAttribute) { //获取自定义缓存键 var cacheKey = CustomCacheKey(invocation); var cacheValue = _cache.GetExists(cacheKey); //判断redis中是否存在值 if (cacheValue) { //将当前获取到的缓存值,赋值给当前执行方法 var type = invocation.Method.ReturnType; var resultTypes = type.GenericTypeArguments; if (type.FullName == "System.Void") { return; } object response; if (type != null && typeof(Task).IsAssignableFrom(type)) { //返回Task<T> if (resultTypes.Any()) { var resultType = resultTypes.FirstOrDefault(); var data = _cache.Get(cacheKey); dynamic temp = JsonConvert.DeserializeObject(data, resultType); //dynamic temp = System.Convert.ChangeType(cacheValue, resultType); // System.Convert.ChangeType(Task.FromResult(temp), type); response = Task.FromResult(temp); } else { //Task 无返回方法 指定时间内不允许重新运行 response = Task.Yield(); } } else { //var data = _redisCache.Get<object>(cacheKey); //response = System.Convert.ChangeType(data, type); var data = _cache.Get(cacheKey); dynamic temp = JsonConvert.DeserializeObject(data, type);//不存task返回类型就直接用返回类型反序列化 response = System.Convert.ChangeType(temp, 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; } _cache.Set(cacheKey, response, TimeSpan.FromMinutes(cacheAttribute.AbsoluteExpiration)); } }