public async Task <List <ForumCategoryWithCounts> > GetAllRootCategoriesAsync(int pageIndex = 0, int pageSize = 50) { var cacheKey = $"RootCategories_{pageIndex}_{pageSize}"; lock (_forumCategoryCacheLock) { if (_forumCategoryCache.Exists <List <ForumCategoryWithCounts> >(cacheKey)) { return(_forumCategoryCache.Get <List <ForumCategoryWithCounts> >(cacheKey)); } } var foundCategories = await _forumcategoriesRepository.GetRootForumCategoryWithCountsAsync(); lock (_forumCategoryCacheLock) { if (_forumCategoryCache.Exists <List <ForumCategoryWithCounts> >(cacheKey)) { //Just in case at the mean time some other request already filled it up! //Rather do this than block the lock for long time return(_forumCategoryCache.Get <List <ForumCategoryWithCounts> >(cacheKey)); } _forumCategoryCache.AddAbsolute(foundCategories, cacheKey, TimeSpan.FromHours(8)); } return(foundCategories); }
public async Task <List <ForumComment> > GetLatestCommentsByCategorySlugAsync(string categorySlug, int pageIndex = 0, int pageSize = 50) { var cacheKey = $"LatestComments_{categorySlug}_{pageIndex}_{pageSize}"; lock (_forumCacheLock) { if (_forumCache.Exists <List <ForumComment> >(cacheKey)) { return(_forumCache.Get <List <ForumComment> >(cacheKey)); } } var found = await _forumcommentsRepository.GetLatestCommentsByCategorySlugAsync(categorySlug, pageIndex, pageSize); lock (_forumCacheLock) { if (_forumCache.Exists <List <ForumComment> >(cacheKey)) { //Just in case at the mean time some other request already filled it up! //Rather do this than block the lock for long time return(_forumCache.Get <List <ForumComment> >(cacheKey)); } _forumCache.AddAbsolute(found, cacheKey, TimeSpan.FromMinutes(5)); } return(found); }
public TEntity Get(object[] keys) { if (!HasCache) { return(Repository.Get(keys)); } var itemCacheKey = GetCacheKey(keys); lock (CacheLock) { if (Cache.Exists <TEntity>(itemCacheKey)) { return(Cache.Get <TEntity>(itemCacheKey)); } } var tEntity = Repository.Get(keys); lock (CacheLock) { if (Cache.Exists <TEntity>(itemCacheKey)) { return(Cache.Get <TEntity>(itemCacheKey)); } else { Cache.AddAbsolute(tEntity, itemCacheKey, TimeSpan.FromMinutes(5)); } } return(tEntity); }