/// <summary> /// Verileri Cacheler gönderilen data cache de yoksa cache e atar varsa geriye cache den döner /// Kullanımı : List<User> items = Cache.Data("test", 10, () => GetUsers()); /// </summary> /// <typeparam name="T">GenericType</typeparam> /// <param name="cache"></param> /// <param name="cacheKey">Cache Adı</param> /// <param name="cacheTimeType"></param> /// <param name="expiration"></param> /// <param name="method"></param> /// <param name="expirationSeconds">Cache in bitme süresi gün bazında</param> /// <returns></returns> public static T Data <T>(this Cache cache, string cacheKey, CacheTimeType cacheTimeType, int expiration, Func <T> method) { var data = cache == null ? default(T) : (T)cache[cacheKey]; //if (data == null) //{ data = method(); if (expiration > 0 && data != null) { lock (sync) { DateTime expirationTime; #region CacheTimeType switch (cacheTimeType) { case CacheTimeType.Millisecond: expirationTime = DateTime.Now.AddMilliseconds(expiration); break; case CacheTimeType.Second: expirationTime = DateTime.Now.AddSeconds(expiration); break; case CacheTimeType.Minute: expirationTime = DateTime.Now.AddMinutes(expiration); break; case CacheTimeType.Day: expirationTime = DateTime.Now.AddDays(expiration); break; case CacheTimeType.Hour: expirationTime = DateTime.Now.AddHours(expiration); break; case CacheTimeType.Month: expirationTime = DateTime.Now.AddMonths(expiration); break; case CacheTimeType.Year: expirationTime = DateTime.Now.AddYears(expiration); break; default: expirationTime = DateTime.Now.AddSeconds(expiration); break; } #endregion if (cache != null) { cache.Insert(cacheKey, data, null, expirationTime, Cache.NoSlidingExpiration); } } } //} return(data); }
/// <summary> /// 计算缓存的时间 /// </summary> /// <param name="type"></param> /// <param name="length"></param> /// <returns></returns> private DateTime GetCacheNewTime(CacheTimeType type, int length) { if (length <= 0) { return(DateTime.MaxValue); } var limitTime = DateTime.Now; switch (type) { case CacheTimeType.Day: limitTime = limitTime.AddDays(length); break; case CacheTimeType.Hour: limitTime = limitTime.AddHours(length); break; case CacheTimeType.Minute: limitTime = limitTime.AddMinutes(length); break; case CacheTimeType.Second: limitTime = limitTime.AddSeconds(length); break; } return(limitTime); }
public virtual List <T> ListCache(string cacheName, CacheTimeType cacheTimeType, int cacheTime, Expression <Func <T, bool> > where, int count) { if (!CacheExtensions.CheckCache(cacheName)) { CacheExtensions.SetCache(cacheName, (object)this.dbset.AsNoTracking <T>().Where <T>(where).Take <T>(count).ToList <T>(), cacheTimeType, cacheTime); } return(CacheExtensions.GetCache <List <T> >(cacheName)); }
public virtual List <T> ListCache(string cacheName, CacheTimeType cacheTimeType, int cacheTime) { if (!CacheExtensions.CheckCache(cacheName)) { CacheExtensions.SetCache(cacheName, (object)this.dbset.AsNoTracking <T>().ToList <T>(), cacheTimeType, cacheTime); } return(CacheExtensions.GetCache <List <T> >(cacheName)); }
public static T GetCacheWithSet <T>(string cacheName, Func <T> valueFunc, CacheTimeType cacheTimeType, int times) { if (!CacheExtensions.CheckCache(cacheName)) { CacheExtensions.SetCache(cacheName, (object)valueFunc(), cacheTimeType, times); } return(CacheExtensions.GetCache <T>(cacheName)); }
public static void SetCache(string cacheName, object value, CacheTimeType cacheTimeType, int cacheTime) { switch (cacheTimeType) { case CacheTimeType.ByMinutes: HttpContext.Current.Cache.Insert(cacheName, value, (CacheDependency)null, DateTime.Now.AddMinutes((double)cacheTime), TimeSpan.Zero); break; case CacheTimeType.ByHours: HttpContext.Current.Cache.Insert(cacheName, value, (CacheDependency)null, DateTime.Now.AddHours((double)cacheTime), TimeSpan.Zero); break; case CacheTimeType.ByDays: HttpContext.Current.Cache.Insert(cacheName, value, (CacheDependency)null, DateTime.Now.AddDays((double)cacheTime), TimeSpan.Zero); break; case CacheTimeType.ByYears: HttpContext.Current.Cache.Insert(cacheName, value, (CacheDependency)null, DateTime.Now.AddYears(cacheTime), TimeSpan.Zero); break; } }
public static long GetCacheTime(long cacheTime, CacheTimeType cacheTimeType) { int cacheTimeMultiplier; switch (cacheTimeType) { case CacheTimeType.Second: cacheTimeMultiplier = 1000; break; case CacheTimeType.Minute: cacheTimeMultiplier = 60000; break; case CacheTimeType.Hour: cacheTimeMultiplier = 3600000; break; default: cacheTimeMultiplier = 1; break; } var result = cacheTime*cacheTimeMultiplier; return result; }
public static MvcHtmlString Cache(this HtmlHelper htmlHelper, string cacheName, Func <object> func, CacheTimeType cacheTimeType, int cacheTime) { if (!CacheExtensions.CheckCache(cacheName)) { CacheExtensions.SetCache(cacheName, (object)func().ToString(), cacheTimeType, cacheTime); } return(MvcHtmlString.Create(CacheExtensions.GetCache <string>(cacheName))); }