public async Task <TItem> GetOrCreateAsync <TItem>( object key, Func <Task <TItem> > factory, CacheDuration cacheDuration) { return(await _keyedLock.RunWithLock(key, () => GetOrCreateAsyncInternal(key, factory, cacheDuration))); }
private async Task <TItem> GetOrCreateAsyncInternal <TItem>( object key, Func <Task <TItem> > factory, CacheDuration cacheDuration) { switch (cacheDuration) { case CacheDuration.None: return(await factory()); case CacheDuration.Short: return(await GetOrCreateAsync(ShortTerm, key, factory)); case CacheDuration.Medium: return(await GetOrCreateAsync(MediumTerm, key, factory)); case CacheDuration.Long: return(await GetOrCreateAsync(LongTerm, key, factory)); case CacheDuration.VeryLong: return(await GetOrCreateAsync(VeryLongTerm, key, factory)); default: throw new ArgumentOutOfRangeException( nameof(cacheDuration), cacheDuration, $"Value '{cacheDuration}' is not implemented."); } }
public string DownloadContent(string data_url, CacheDuration cache_duration) { string [] split_data_url = data_url.Split ('/'); string fname = split_data_url[split_data_url.Length - 2] + Path.GetExtension (data_url); return DownloadContent (data_url, GetCachedPath (fname), cache_duration); }
/// <summary> /// 添加缓存 指定缓存时间 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="cacheDuration"></param> public void Set <T>(string key, object value, CacheDuration cacheDuration) { string serstr = JsonConvert.SerializeObject(value); if (value == null) { throw new ArgumentNullException("value", "Cannot cache a null object."); } switch (cacheDuration) { case CacheDuration.None: client.Set <T>(GetCacheKey(key), serstr, DateTime.Now.AddMinutes((int)cacheDuration)); break; case CacheDuration.Hour: client.Set <T>(GetCacheKey(key), serstr, DateTime.Now.AddMinutes((int)cacheDuration)); break; case CacheDuration.Day: client.Set <T>(GetCacheKey(key), serstr, DateTime.Now.AddDays((int)cacheDuration)); break; case CacheDuration.Week: client.Set <T>(GetCacheKey(key), serstr, DateTime.Now.AddDays((int)cacheDuration)); break; case CacheDuration.Month: client.Set(GetCacheKey(key), value, DateTime.Now.AddDays((int)cacheDuration)); break; default: client.Set <T>(GetCacheKey(key), serstr, DateTime.Now.AddMinutes((int)cacheDuration)); break; } }
public async Task <TCache> StoreAndGetAsync <TCache>(string key, Func <Task <TCache> > cacheFactory, CacheDuration duration = CacheDuration.Eternal) { string oldCache = await _multiplexer.Db.StringGetAsync(key); _logger.Information($"cache object: {typeof(TCache).Name} \n"); if (string.IsNullOrWhiteSpace(oldCache)) { try { var toBeCached = await cacheFactory(); var result = await StoreAsync(key, toBeCached, duration); return(result); } catch (Exception e) { _logger.Fatal($"create object to be cached failed: {typeof(TCache).Name} \n" + $"exception : {e}"); throw; } } return(JsonSerializer.Deserialize <TCache>(oldCache, _serializerOptions)); }
public string DownloadContent(string data_url, CacheDuration cache_duration) { string [] split_data_url = data_url.Split('/'); string fname = split_data_url[split_data_url.Length - 2] + Path.GetExtension(data_url); return(DownloadContent(data_url, GetCachedPath(fname), cache_duration)); }
public void Refresh() { CacheDuration old_duration = cache_duration; cache_duration = CacheDuration.None; GetData(); cache_duration = old_duration; }
//public static void Add(object objectToCache, string cacheKey, CacheDuration duration) //{ // DateTime expirationDate; // switch (duration) // { // case CacheDuration.Week: // expirationDate = DateTime.Now.AddDays(7); // break; // case CacheDuration.Day: // expirationDate = DateTime.Now.AddDays(1); // break; // case CacheDuration.Hour: // expirationDate = DateTime.Now.AddHours(1); // break; // case CacheDuration.HalfDay: // expirationDate = DateTime.Now.AddHours(12); // break; // case CacheDuration.HalfHour: // expirationDate = DateTime.Now.AddMinutes(30); // break; // case CacheDuration.TenSeconds: // expirationDate = DateTime.Now.AddSeconds(10); // break; // case CacheDuration.ThreeMinutes: // expirationDate = DateTime.Now.AddMinutes(3); // break; // case CacheDuration.Minute: // expirationDate = DateTime.Now.AddMinutes(1); // break; // default: // expirationDate = DateTime.Now; // break; // } // if (HttpRuntime.Cache != null) // { // HttpRuntime.Cache.Add(cacheKey, objectToCache, null, expirationDate, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, null); // } // //HttpContext.Current.Cache.Insert(cacheKey, objectToCache, null, expirationDate, System.Web.Caching.Cache.NoSlidingExpiration); //} public static void Add(object objectToCache, string cacheKey, CacheDuration duration) { DateTime expirationDate; switch (duration) { case CacheDuration.Week: expirationDate = DateTime.Now.AddDays(7); break; case CacheDuration.Day: expirationDate = DateTime.Now.AddDays(1); break; case CacheDuration.Hour: expirationDate = DateTime.Now.AddHours(1); break; case CacheDuration.HalfDay: expirationDate = DateTime.Now.AddHours(12); break; case CacheDuration.HalfHour: expirationDate = DateTime.Now.AddMinutes(30); break; case CacheDuration.TenSeconds: expirationDate = DateTime.Now.AddSeconds(10); break; case CacheDuration.TwentyFiveSec: expirationDate = DateTime.Now.AddSeconds(25); break; case CacheDuration.ThreeMinutes: expirationDate = DateTime.Now.AddMinutes(3); break; case CacheDuration.TenMinutes: expirationDate = DateTime.Now.AddMinutes(10); break; case CacheDuration.Minute: expirationDate = DateTime.Now.AddMinutes(1); break; default: expirationDate = DateTime.Now; break; } if (HttpContext.Current != null) { HttpContext.Current.Cache.Insert(cacheKey, objectToCache, null, expirationDate, System.Web.Caching.Cache.NoSlidingExpiration); } }
private StringBuilder CreateRawCacheKey() { // Note: The cache key will contain information such as server names and // passwords, however it will be stored in the internal cache, which is // not accessible to page developers, so it is secure. StringBuilder sb = new StringBuilder(CacheInternal.PrefixDataSourceControl, 1024); sb.Append(GetType().GetHashCode().ToString(CultureInfo.InvariantCulture)); sb.Append(CacheDuration.ToString(CultureInfo.InvariantCulture)); sb.Append(':'); sb.Append(((int)CacheExpirationPolicy).ToString(CultureInfo.InvariantCulture)); SqlDataSourceCache sqlCache = Cache as SqlDataSourceCache; if (sqlCache != null) { sb.Append(":"); sb.Append(sqlCache.SqlCacheDependency); } sb.Append(":"); sb.Append(ConnectionString); sb.Append(":"); sb.Append(SelectCommand); //sb.Append(SelectCountCommand); // Append parameter names and values if (SelectParameters.Count > 0) { sb.Append("?"); IDictionary parameters = SelectParameters.GetValues(Context, this); foreach (DictionaryEntry entry in parameters) { sb.Append(entry.Key.ToString()); if ((entry.Value != null) && (entry.Value != DBNull.Value)) { sb.Append("="); sb.Append(entry.Value.ToString()); } else { if (entry.Value == DBNull.Value) { sb.Append("(dbnull)"); } else { sb.Append("(null)"); } } sb.Append("&"); } } return(sb); }
public async Task <T> Get <T>(string url, CacheDuration cacheDuration = CacheDuration.None) { if (url == null) { throw new ArgumentNullException(nameof(url)); } var requestUri = GetRequestUri(url); return(await _cache.GetOrCreateAsync(requestUri, () => GetInternal <T>(requestUri), cacheDuration)); }
public static void Add(object objectToCache, string cacheKey, CacheDuration duration) { DateTime expirationDate; switch (duration) { case CacheDuration.Week: expirationDate = DateTime.Now.AddDays(7); break; case CacheDuration.Day: expirationDate = DateTime.Now.AddDays(1); break; case CacheDuration.Hour: expirationDate = DateTime.Now.AddHours(1); break; case CacheDuration.HalfDay: expirationDate = DateTime.Now.AddHours(12); break; case CacheDuration.SixHours: expirationDate = DateTime.Now.AddHours(12); break; case CacheDuration.HalfHour: expirationDate = DateTime.Now.AddMinutes(30); break; case CacheDuration.TenSeconds: expirationDate = DateTime.Now.AddSeconds(10); break; case CacheDuration.TenMinutes: expirationDate = DateTime.Now.AddMinutes(10); break; case CacheDuration.ThreeMinutes: expirationDate = DateTime.Now.AddMinutes(3); break; default: expirationDate = DateTime.Now; break; } if (HttpRuntime.Cache != null) { HttpRuntime.Cache.Add(cacheKey, objectToCache, null, expirationDate, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, null); } }
public void Add(ICacheKey key, object value, CacheDuration cacheDuration) { if (this.ShouldCache(key)) { this.cache.Add( (CacheKey)key, value, null, DateTime.Now.AddSeconds(this.DurationInSeconds(cacheDuration)), TimeSpan.Zero, CacheItemPriority.Normal, null); } }
internal static string DownloadContent(string data_url, string cache_file, CacheDuration cache_duration) { if (String.IsNullOrEmpty(data_url) || String.IsNullOrEmpty(cache_file)) { return(null); } data_url = FixLastfmUrl(data_url); // See if we have a valid cached copy if (cache_duration != CacheDuration.None) { if (File.Exists(cache_file)) { DateTime last_updated_time = File.GetLastWriteTime(cache_file); if (cache_duration == CacheDuration.Infinite || DateTime.Now - last_updated_time < DataCore.NormalCacheTime) { return(cache_file); } } } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(data_url); request.UserAgent = DataCore.UserAgent; request.KeepAlive = false; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = GetResponseStream(response)) { using (FileStream file_stream = File.Open(cache_file, FileMode.Create)) { using (BufferedStream buffered_stream = new BufferedStream(file_stream)) { byte [] buffer = new byte[8192]; int read; while (true) { read = stream.Read(buffer, 0, buffer.Length); if (read <= 0) { break; } buffered_stream.Write(buffer, 0, read); } } } } } return(cache_file); }
public LastfmData(string dataUrlFragment, CacheDuration cacheDuration, string xpath) { DataCore.Initialize(); this.data_url = DataCore.FixLastfmUrl(String.Format("http://ws.audioscrobbler.com/1.0/{0}", dataUrlFragment)); this.cache_file = DataCore.GetCachedPathFromUrl(data_url); this.cache_duration = cacheDuration; this.xpath = xpath; try { GetData(); } catch { Refresh(); } }
public void PutObject(string cacheKey, object cacheObject, CacheDuration cacheDuration = CacheDuration.CacheDefault) { _logger.Debug("Storing item with key: {0} in cache with duration: {1}", cacheKey, cacheDuration); if (cacheObject == null) { return; } var cacheTimeSpan = TimeSpan.FromMinutes((int)cacheDuration); var value = JsonConvert.SerializeObject(cacheObject); Cache.StringSet(cacheKey, value, cacheTimeSpan); _logger.Debug("Stored item with key: {0} in cache with timespan: {1}", cacheKey, cacheTimeSpan); }
public void CacheObject(String key, Object forObject, CacheDuration duration, Boolean isSlidingTime) { lock (syncRoot) { if (cache.ContainsKey(key)) { cache.Remove(key); } // REMOVE EXISTING CacheObject cacheObject; Double durationSeconds = dataCacheDurationInitial * Math.Pow(dataCacheDurationMultiplier, ((Int32)duration)); cacheObject = new CacheObject(forObject, durationSeconds, isSlidingTime); cache.Add(key, cacheObject); } return; }
private void Store(string key, object value, CacheDuration cacheDuration) { _logger.Debug("Storing item with key: {0} in cache with duration: {1}", key, cacheDuration); if (value == null) { return; } TimeSpan cacheTimeSpan = TimeSpan.FromMinutes((int)cacheDuration); var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.Add(cacheTimeSpan) }; _cache.Set(key, value, policy); _logger.Debug("Stored item with key: {0} in cache with timespan: {1}", key, cacheTimeSpan); }
public async Task <TCache> StoreAsync <TCache>(string key, TCache toBeCached, CacheDuration duration = CacheDuration.Eternal) { try { string objectString = JsonSerializer.Serialize(toBeCached, _serializerOptions); await _multiplexer.Db.StringSetAsync(key, objectString, duration == CacheDuration.Eternal ?(TimeSpan?)null : TimeSpan.FromSeconds((int)duration)); return(toBeCached); } catch (Exception e) { _logger.Fatal($"object cached failed: {typeof(TCache).Name} \n" + $"exception : {e}"); throw; } }
private void Store(string key, object value, CacheDuration cacheDuration) { _logger.Debug("Storing item with key: {0} in cache with duration: {1}", key, cacheDuration); if (value == null) { return; } TimeSpan cacheTimeSpan = TimeSpan.Zero; try { if (cacheDuration != CacheDuration.CacheDefault) { cacheTimeSpan = TimeSpan.FromMinutes((int)cacheDuration); _cache.Put(key, value, cacheTimeSpan); } else { _cache.Put(key, value); } } catch (Exception cacheException) { if (cacheTimeSpan != TimeSpan.Zero) { _logger.Warn("Attempt to store item in cache with key: " + key + " with expiry timespan: " + cacheTimeSpan, cacheException); } else { _logger.Warn("Attempt to store item in cache with key: " + key + " using cache default eviction policy", cacheException); } return; } _logger.Debug("Stored item with key: {0} in cache with timespan: {1}", key, cacheTimeSpan); }
/// <summary> /// store data in application cache /// </summary> /// <param name="Key">name of the unique key</param> /// <param name="_Object">value to be stored</param> /// <param name="_CacheDuration">life span of the cache</param> public static void SetCache(string Key, Object _Object, CacheDuration _CacheDuration) { try { //if (Key != null && HttpContext.Current != null && _Object != null) //{ // if (_CacheDuration != CacheDuration.VeryHigh) // { // HttpContext.Current.Cache.Add(Key, _Object, null, DateTime.Now.AddSeconds(_CacheDuration.GetHashCode()), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); // } // else // { // HttpContext.Current.Cache.Add(Key, _Object, null, DateTime.Now.AddMinutes(_CacheDuration.GetHashCode()), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); // } //} } catch (Exception ex) { AggieGlobalLogManager.Fatal(ex, "Utility:SetCache(): Failed to set cache for key={0} , data={1}", Key, _Object); } }
internal static string DownloadContent(string data_url, string cache_file, CacheDuration cache_duration) { SafeUri uri = new SafeUri(cache_file); if (String.IsNullOrEmpty(data_url) || String.IsNullOrEmpty(cache_file)) { return(null); } // See if we have a valid cached copy if (cache_duration != CacheDuration.None) { if (Banshee.IO.File.Exists(uri)) { DateTime last_updated_time = DateTime.FromFileTime(Banshee.IO.File.GetModifiedTime(uri)); if (cache_duration == CacheDuration.Infinite || DateTime.Now - last_updated_time < DataFetch.NormalCacheTime) { return(cache_file); } } } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(data_url); request.UserAgent = DataFetch.UserAgent; request.KeepAlive = false; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Banshee.IO.StreamAssist.Save(GetResponseStream(response), Banshee.IO.File.OpenWrite(uri, true)); } } catch (Exception e) { Log.DebugException(e); cache_file = null; } return(cache_file); }
public async Task <T> GetList <T, TValue>( string url, CacheDuration cacheDuration = CacheDuration.None, int?maxResultCount = null, int takeCount = 500) where T : ListApiResponseBase <TValue> { if (url == null) { throw new ArgumentNullException(nameof(url)); } var separator = url.Contains("?") ? "&" : "?"; var model = await Get <T>($"{url}{separator}$top={takeCount}", cacheDuration); var modelCount = model.Count; var pageIndex = 1; while (modelCount >= takeCount && maxResultCount != null && maxResultCount < modelCount) { var skipCount = takeCount * pageIndex; var pagedUrl = $"{url}{separator}$skip={skipCount}"; var pagedModel = await Get <T>(pagedUrl, cacheDuration); model.Count += pagedModel.Count; pagedModel.Value.ToList().ForEach(x => model.Value.Add(x)); modelCount = pagedModel.Count; pageIndex++; } return(model); }
public static string DownloadContent(string data_url, CacheDuration cache_duration) { return DownloadContent (data_url, GetCachedPathFromUrl (data_url), cache_duration); }
public CachedAttribute(CacheDuration duration = CacheDuration.Short_10min) { _duration = duration; }
public T Get <T>(string key, Func <T> onCacheExpire, CacheDuration cacheDuration) where T : class { return(TryGetValue(key, onCacheExpire, (int)cacheDuration)); }
public LastfmData(string dataUrlFragment, CacheDuration cacheDuration) : this(dataUrlFragment, cacheDuration, null) { }
/// <summary> /// <para>Inserts the specified object to the <see cref="System.Web.Caching.Cache"/> object /// with a cache key to reference its location and using default values provided by /// the <see cref="System.Web.Caching.CacheItemPriority"/> enumeration. /// </para> /// <para> /// Allows specifying a general cache duration. /// </para> /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="cacheDuration">The cache duration.</param> public void Insert(string key, object value, CacheDuration cacheDuration) { if(value == null) throw new ArgumentNullException("value", "Cannot cache a null object."); this.cache.Insert(GetCacheKey(key), value, null, DateTime.Now.AddSeconds((int)cacheDuration), TimeSpan.Zero, CacheItemPriority.Normal, null); }
internal static string DownloadContent(string data_url, string cache_file, CacheDuration cache_duration) { if (String.IsNullOrEmpty (data_url) || String.IsNullOrEmpty (cache_file)) { return null; } data_url = FixLastfmUrl (data_url); // See if we have a valid cached copy if (cache_duration != CacheDuration.None) { if (File.Exists (cache_file)) { DateTime last_updated_time = File.GetLastWriteTime (cache_file); if (cache_duration == CacheDuration.Infinite || DateTime.Now - last_updated_time < DataCore.NormalCacheTime) { return cache_file; } } } HttpWebRequest request = (HttpWebRequest) WebRequest.Create (data_url); request.UserAgent = DataCore.UserAgent; request.KeepAlive = false; using (HttpWebResponse response = (HttpWebResponse) request.GetResponse ()) { using (Stream stream = GetResponseStream (response)) { using (FileStream file_stream = File.Open (cache_file, FileMode.Create)) { using (BufferedStream buffered_stream = new BufferedStream (file_stream)) { byte [] buffer = new byte[8192]; int read; while (true) { read = stream.Read (buffer, 0, buffer.Length); if (read <= 0) { break; } buffered_stream.Write (buffer, 0, read); } } } } } return cache_file; }
private int DurationInSeconds(CacheDuration duration) { return this.cacheDurations[duration]; }
internal string CreateCacheKey() { StringBuilder sb = new StringBuilder(CacheInternal.PrefixDataSourceControl, 1024); sb.Append(GetType().GetHashCode().ToString(CultureInfo.InvariantCulture)); sb.Append(CacheDuration.ToString(CultureInfo.InvariantCulture)); sb.Append(':'); sb.Append(((int)CacheExpirationPolicy).ToString(CultureInfo.InvariantCulture)); bool includeUniqueID = false; if (!String.IsNullOrEmpty(CacheKeyContext)) { sb.Append(':'); sb.Append(CacheKeyContext); } if (DataFile.Length > 0) { sb.Append(':'); sb.Append(DataFile); } else { if (Data.Length > 0) { includeUniqueID = true; } } if (TransformFile.Length > 0) { sb.Append(':'); sb.Append(TransformFile); } else { if (Transform.Length > 0) { includeUniqueID = true; } } if (includeUniqueID) { // If we don't have any paths, use the Page if (Page != null) { sb.Append(':'); sb.Append(Page.GetType().AssemblyQualifiedName); } sb.Append(':'); string uniqueID = UniqueID; if (String.IsNullOrEmpty(uniqueID)) { throw new InvalidOperationException(SR.GetString(SR.XmlDataSource_NeedUniqueIDForCache)); } sb.Append(uniqueID); } return(sb.ToString()); }
public void PutObject(string cacheKey, object cacheObject, CacheDuration cacheDuration = CacheDuration.CacheDefault) { Store(cacheKey, cacheObject, cacheDuration); }
internal static string DownloadContent(string data_url, string cache_file, CacheDuration cache_duration) { SafeUri uri = new SafeUri (cache_file); if (String.IsNullOrEmpty (data_url) || String.IsNullOrEmpty (cache_file)) { return null; } // See if we have a valid cached copy if (cache_duration != CacheDuration.None) { if (Banshee.IO.File.Exists (uri)) { DateTime last_updated_time = DateTime.FromFileTime (Banshee.IO.File.GetModifiedTime (uri)); if (cache_duration == CacheDuration.Infinite || DateTime.Now - last_updated_time < DataFetch.NormalCacheTime) { return cache_file; } } } HttpWebRequest request = (HttpWebRequest) WebRequest.Create (data_url); request.UserAgent = DataFetch.UserAgent; request.KeepAlive = false; try { using (HttpWebResponse response = (HttpWebResponse) request.GetResponse ()) { Banshee.IO.StreamAssist.Save (GetResponseStream (response), Banshee.IO.File.OpenWrite (uri, true)); } } catch (Exception e) { Log.DebugException (e); cache_file = null; } return cache_file; }
private int DurationInSeconds(CacheDuration duration) { return(this.cacheDurations[duration]); }
public static string DownloadContent(string data_url, CacheDuration cache_duration) { return(DownloadContent(data_url, GetCachedPathFromUrl(data_url), cache_duration)); }