/// <summary> /// 查询 /// </summary> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <param name="type"></param> /// <returns></returns> public async Task <NewsQueryDto> Query(string channelId = ShowApiNewsChannel.Domestic, int type = 0, int pageIndex = 1, int pageSize = 20) { var redisKey = $"{RedisTitle}{type}{channelId}_{pageIndex}_{pageSize}"; var dto = redisCache.GetValue <NewsQueryDto>(redisKey); if (dto != null && dto.List.Any()) { return(dto); } var now = DateTime.Now; var first = newsRepository.FirstOrDefault(x => x.ChannelId == channelId && x.Type == type, x => x.Date); if (first == null || (first != null && (now - first.Date).TotalMinutes >= 30)) { await newsApplicationService.InsertNews(channelId, type, pageIndex, pageSize); } var result = GetNews(channelId, pageIndex, pageSize); redisCache.SetValue(redisKey, result, TimeSpan.FromMinutes(5)); return(result); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <TodayOnHistoryDetailDto> Detail(int id) { var dto = redisCache.GetValue <TodayOnHistoryDetailDto>($"{RedisTitle}{id}"); if (dto != null) { return(dto); } var model = todayOnHistoryRepository.Detail(id); if (model == null) { await InsertDetail(id); model = todayOnHistoryRepository.Detail(id); } var result = mapper.Map <TodayOnHistoryDetailDto>(model); redisCache.SetValue($"{RedisTitle}{id}", result, TimeSpan.FromDays(180)); return(result); }
/// <summary> /// /// </summary> /// <param name="city"></param> /// <returns></returns> public async Task <WeatherQueryDto> Query(string city) { var dto = redisCache.GetValue <WeatherQueryDto>($"{RedisTitle}{city}"); if (dto != null) { return(dto); } var weather = weatherRepository.Query(city); if (weather == null) { await InsertWeather(city); weather = weatherRepository.Query(city); } else if ((DateTime.Now - weather.AddDate).TotalHours >= 6) { await Update(city); weather = weatherRepository.Query(city); } var result = new WeatherQueryDto { City = weather.City, RealTime = mapper.Map <RealTimeWeatherQueryDto>(weather), Future = weather.Future // JsonConvert.DeserializeObject<List<FutureWeatherQueryDto>>(weather.Future) }; redisCache.SetValue($"{RedisTitle}{city}", result, TimeSpan.FromHours(6)); return(result); //return mapper.Map<WeatherQueryDto>(weather); }
public void Intercept(IInvocation invocation) { var cacheKey = ""; object response; var returnType = invocation.Method.ReturnType; if (returnType.FullName == "System.Void") { return; } var method = invocation.MethodInvocationTarget ?? invocation.Method; if (method.GetCustomAttributes(true).Any(m => m.GetType() == typeof(CachingAttribute))) { //获取自定义缓存键 cacheKey = CustomCacheKey(invocation); //根据key获取相应的缓存值 var cacheValue = _cache.GetValue(cacheKey); if (cacheValue != null) { var returnTypeArguments = returnType.GenericTypeArguments; if (typeof(Task).IsAssignableFrom(returnType)) { //返回Task<T> if (returnTypeArguments.Length > 0) { var returnTypeArgument = returnTypeArguments.FirstOrDefault(); // 核心1,直接获取 dynamic 类型 dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, returnTypeArgument); //dynamic temp = System.Convert.ChangeType(cacheValue, resultType); // System.Convert.ChangeType(Task.FromResult(temp), type); response = Task.FromResult(temp); } else { response = Task.Yield(); } } else { // 核心2,要进行 ChangeType response = System.Convert.ChangeType(_cache.Get <object>(cacheKey), returnType); } //将当前获取到的缓存值,赋值给当前执行方法 invocation.ReturnValue = response; return; } } //去执行当前的方法 invocation.Proceed(); //存入缓存 if (!string.IsNullOrWhiteSpace(cacheKey)) { if (typeof(Task).IsAssignableFrom(returnType)) { var resultProperty = returnType.GetProperty("Result"); response = resultProperty.GetValue(invocation.ReturnValue); } else { response = invocation.ReturnValue; } if (response == null) { response = string.Empty; } var abs = ((CachingAttribute)method.GetCustomAttributes(true) .FirstOrDefault(m => m.GetType() == typeof(CachingAttribute)))?.AbsoluteExpiration ?? 10; _cache.Set(cacheKey, response, TimeSpan.FromMinutes(abs)); } }
public IReadOnlyList <Film> GetRecommendations(int userId) { return(redisCahce.GetValue(GetKey(userId), () => GetRecommendationsInternal(userId), TimeSpan.FromMinutes(1))); }
//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();//直接执行被拦截方法 } }