private readonly ICacheManager _cacheManager; public ProgrammeManage(ICacheManager cacheManager) { _cacheManager = cacheManager; } /// <summary> /// 定时任务:更新缓存(仅查询今年的投票结果、按照节目顺序排序) /// </summary> /// <returns></returns> public async Task SaveVoteResult() { GetAllProgrammeInfosByYearOutput result = GetVoteResult(DateTime.Now.Year).Result; await _cacheManager.GetCache("GetAllProgrammeInfosByYearOutput") .SetAsync("GetAllProgrammeInfosByYearOutput", result); } /// <summary> /// 查询缓存 /// </summary> /// <param name="inputYear"></param> /// <param name="sortProperty"></param> /// <returns></returns> public async Task<GetAllProgrammeInfosByYearOutput> GetVoteResult(long inputYear,string sortProperty) { var cache = _cacheManager .GetCache("GetAllProgrammeInfosByYearOutput"); long year = inputYear == 0 ? DateTime.Now.Year : inputYear; //先尝试读取缓存,缓存没有则读取数据库 var result =await cache.GetAsync("GetAllProgrammeInfosByYearOutput", async ()=> { return await GetVoteResult(year); }) as GetAllProgrammeInfosByYearOutput; return result; }
原文地址:https://www.cnblogs.com/Lulus/p/8341459.html
时间: 2024-11-05 18:50:49