public async Task <ThreadInfo> GetThreadInfoRootCategory(string categorySlug, string threadSlug) { var cacheKey = $"ThreadInfoRootCategory_{categorySlug}_{threadSlug}"; lock (_forumCacheLock) { if (_forumCache.Exists <ThreadInfo>(cacheKey)) { return(_forumCache.Get <ThreadInfo> (cacheKey)); } } var found = await _forumcommentsRepository.GetThreadInfoRootCategory(categorySlug, threadSlug); lock (_forumCacheLock) { if (_forumCache.Exists <ThreadInfo>(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 <ThreadInfo>(cacheKey)); } _forumCache.AddSliding(found, cacheKey, TimeSpan.FromHours(5)); } return(found); }
/* * private void AddCacheUIntDataEntity(TEntity entity) * { * var dataEntity = entity as UIntDataEntity; * if (dataEntity != null) * { * // ReSharper disable once PossibleInvalidOperationException * var itemCacheKey = GetCacheKey(new object[] {dataEntity.DatabaseId.Value}); * lock (CacheLock) * { * Cache.Clear(itemCacheKey); * Cache.AddSliding(entity, itemCacheKey, TimeSpan.FromMinutes(5)); * } * } * } */ private void AddCacheIntDataEntity(TEntity entity) { //var dataEntity = entity as IntDataEntity; var dataEntity = entity; if (dataEntity != null) { // ReSharper disable once PossibleInvalidOperationException var itemCacheKey = GetCacheKey(new object[] { dataEntity.Id.Value }); lock (CacheLock) { Cache.Clear(itemCacheKey); Cache.AddSliding(entity, itemCacheKey, TimeSpan.FromMinutes(5)); } } }