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); }
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 <ForumThreadWithCounts> > GetForumThreadsByCategorySlugAsync(string categorySlug, int pageIndex = 0, int pageSize = 50) { var cacheKey = $"ThreadsOfSlug_{categorySlug}_{pageIndex}_{pageSize}"; lock (_forumCacheLock) { if (_forumCache.Exists <List <ForumThreadWithCounts> >(cacheKey)) { return(_forumCache.Get <List <ForumThreadWithCounts> >(cacheKey)); } } var foundThreads = await _forumthreadsRepository.GetForumThreadsByCategorySlugAsync(categorySlug, pageIndex, pageSize); lock (_forumCacheLock) { if (_forumCache.Exists <List <ForumThread> >(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 <ForumThreadWithCounts> >(cacheKey)); } _forumCache.AddAbsolute(foundThreads, cacheKey, TimeSpan.FromMinutes(15)); } return(foundThreads); }
private async Task <Tuple <int, Size, string> > GetImageSize(WebClient client, string uri, int index) { if (_imageSizeCache.Exists(uri)) { var cached = _imageSizeCache.Get(uri); return(cached == Size.Empty ? null : Tuple.Create(index, cached, uri)); } var size = Size.Empty; try { using (var stream = await client.OpenReadTaskAsync(uri)) { var image = Image.FromStream(stream); size = IsAnimationGif(image) ? Size.Empty : image.Size; } } catch (Exception) { size = Size.Empty; } _imageSizeCache.Add(uri, size, 1440); // 24時間 return(size == Size.Empty ? null : Tuple.Create(index, size, uri)); }
public async Task <ICollection <RefViewModel> > ReadRefViewModels(long refListId) { var cacheKey = refListId.ToString(); if (!_refViewModelsCache.Exists(cacheKey)) { await UpdateCache(refListId); } return(_refViewModelsCache.Get(cacheKey)); }
/// <summary> /// Checks if a key exists in this cache /// </summary> /// <param name="Key">Item Key</param> /// <param name="Recurse">If true, also searches all OverflowCaches</param> /// <returns>True if the object exists</returns> public bool Exists(ComparableBytesAbstract Key, bool Recurse) { bool e = Exists(Key); if (Recurse && e == false && OverflowCache != null) { return(OverflowCache.Exists(Key, Recurse)); } return(e); }
/// <summary> /// HttpClientではhttp://www.yomiuri.co.jp/で文字化けが起こった。(Shift_JISだから?) /// </summary> private async Task <string> GetHtml(string url) { if (_htmlCache.Exists(url)) { return(_htmlCache.Get(url)); } if (url == null) { throw new ArgumentNullException("url"); } var result = default(Uri); if (!Uri.TryCreate(url, UriKind.Absolute, out result)) { throw new ArgumentException(string.Format("Illegal url format: {0}", url)); } var client = new WebClient(); var data = await client.DownloadDataTaskAsync(url); var html = ""; var headerContentTypeValue = client.ResponseHeaders[HttpResponseHeader.ContentType]; if (!string.IsNullOrWhiteSpace(headerContentTypeValue)) { var contentType = new ContentType(headerContentTypeValue); if (contentType.CharSet != null) { html = Encoding.GetEncoding(contentType.CharSet).GetString(data); } } if (string.IsNullOrWhiteSpace(html)) { var ascii = Encoding.ASCII.GetString(data); var match = MetaCharsetRegex.Match(ascii); var metaCharsetValue = match.Success ? match.Groups[1].Value : ""; if (!string.IsNullOrWhiteSpace(metaCharsetValue)) { try { var enc = Encoding.GetEncoding(metaCharsetValue); html = enc.GetString(data); } catch (ArgumentException) { /// Encoding.GetEncoding(metaCharsetValue)で例外が起きた場合、例外処理せずに次の処理に行く } } } if (string.IsNullOrWhiteSpace(html)) { var ascii = Encoding.ASCII.GetString(data); var match = MetaHttpEquivContentTypeRegex.Match(ascii); var metaContentTypeValue = match.Success ? match.Groups[1].Value : ""; if (!string.IsNullOrWhiteSpace(metaContentTypeValue)) { var contentType = new ContentType(metaContentTypeValue); if (contentType.CharSet != null) { html = Encoding.GetEncoding(contentType.CharSet).GetString(data); } } } if (string.IsNullOrWhiteSpace(html)) { html = Encoding.UTF8.GetString(data); } if (!string.IsNullOrWhiteSpace(html)) { _htmlCache.Add(url, html, 1440); // 24時間 } return(html); }
public bool Exists(Func <TValue, bool> predicate) { return(_index.Exists(predicate)); }