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 T ExecuteWithCache <T>(string key, Func <T> func, DateTimeOffset expiryOffset, params object[] cacheByParams) { if (cacheByParams != null && cacheByParams.Any()) { var hash = JsonConvert.SerializeObject(cacheByParams).GetHashCode(); key += hash; } var cachedResult = _objectCache.Get(key); if (cachedResult != null) { if (cachedResult is T) { return((T)cachedResult); } return((T)Convert.ChangeType(cachedResult, typeof(T))); } var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = expiryOffset; var result = func(); _objectCache.Add(key, result, policy); return(result); }
public override object CreateInstance(TypeId id) { var registration = _typeCache.Find(id); if (registration == null) { return(null); } var o = CreateObject( registration.GetConstructionType(id.Type), registration.Dependencies); if (registration.ShouldCache) { _objectCache.Add(id, o); } return(o); }
private async Task UpdateCache(long id) { var cacheKey = id.ToString(); var list = await _refListHandler.GetRefListAsync(id); if (list != null) { var listViewModel = Mapper.Map <RefListViewModel>(list); var refViewModels = Mapper.Map <ICollection <RefViewModel> >(list.Refs); _refListViewModelCache.Add(cacheKey, listViewModel, 10); _refViewModelsCache.Add(cacheKey, refViewModels, 10); } }
/// <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 void Add <T, T2, T3>(List <T> items) where T : BaseDataEntity <T2, T3> { Dictionary <T2, T> dictionary = items.ToDictionary(item => item.ItemId); _objectCache.Add(typeof(T).Name, dictionary, DateTime.Now.AddDays(2)); }