public void SetCategoryList(object category = null) { if (!_cacheManager.IsSet("CategoriList_")) { _cacheManager.Add("CategoriList_", _categoryRepository.GetMany(x => x.ParentCategoryId == 0).ToList(), 3600); } var categoryList = _cacheManager.Get <List <Category> >("CategoriList_"); ViewBag.CategoryList = categoryList; }
public static T Get <T>(this IMemoryCacheManager cacheManager, string key, Func <T> acquire, int cacheTime = 60) { if (cacheManager.IsSet(key)) { return(cacheManager.Get <T>(key)); } else { var result = acquire(); cacheManager.Set(key, result, cacheTime); return(result); } }
public async Task <T> GetItemFromHashAsync <T>(string redisKey, string redisHashKey) { var compareKey = GenerateKey(redisKey + redisHashKey); if (_memoryCacheManager.IsSet(compareKey)) { return(_memoryCacheManager.Get <T>(compareKey)); } var rValue = await _db.HashGetAsync(GenerateKey(redisKey), redisHashKey); if (!rValue.HasValue) { _memoryCacheManager.Set(compareKey, 0, MemoryCacheTime); return(default(T)); } var resultValue = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(rValue); _memoryCacheManager.Set(compareKey, resultValue, MemoryCacheTime); return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(rValue)); }