public void AddOrReplace(string key, object value) { Ensure.StringArgumentNotNullAndNotEmpty(key, nameof(key)); Ensure.ArgumentNotNull(value, nameof(value)); System.Runtime.Caching.CacheItem item = new System.Runtime.Caching.CacheItem(key, value); System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy(); policy.RemovedCallback = x => OnCacheItemRemoved(new CacheItemRemovedEventArgs(x.CacheItem.Key, x.CacheItem.Value)); cache.Set(item, policy); }
public T Set <T>(string key, CacheInfo <T> cache) where T : class { var oldCache = _cache.Get(key); var cacheItem = new System.Runtime.Caching.CacheItem(key, cache.Value); var policy = new System.Runtime.Caching.CacheItemPolicy(); policy.SlidingExpiration = cache.SlidingExpiration; _cache.Set(cacheItem, policy); return(oldCache == null ? null : (T)oldCache); }
public bool AddItem(System.Runtime.Caching.CacheItem item, int seconds = -1) { if (seconds > 0) { TimeSpan ts = new TimeSpan(seconds * 1000); return(client.Store(StoreMode.Add, item.Key, item.Value, ts)); } else { return(client.Store(StoreMode.Add, item.Key, item.Value)); } }
/// <summary> /// 获取数据缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="CacheKey">键</param> /// lamda表达式:可以是() => new BLL.CangFangList().Single(CangFangId)或者() => "abc" /// 或者委托(注意返回): delegate() { return new BLL.CangFangList().Single(CangFangId); } /// <param name="slidingExpiration">用于设置可调过期时间,它表示当离最后访问超过某个时间段后就过期new TimeSpan(0, 10, 0)或者TimeSpan.FromMinutes(60)</param> /// <param name="absoluteExpiration">用于设置绝对过期时间,System.DateTime.Now.AddMinutes(20)</param> /// <returns></returns> public static T GetCache <T>(string CacheKey, Func <T> cachePopulate, TimeSpan slidingExpiration, DateTime absoluteExpiration) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; object objObject = objCache[CacheKey]; if (objObject == null) { var item = new System.Runtime.Caching.CacheItem(CacheKey, cachePopulate()); SetCache(CacheKey, item, absoluteExpiration, slidingExpiration); } return((T)objCache[CacheKey]); }
public CacheEntryRemovedArguments(ObjectCache source, CacheEntryRemovedReason reason, System.Runtime.Caching.CacheItem cacheItem) { if (source == null) { throw new ArgumentNullException("source"); } if (cacheItem == null) { throw new ArgumentNullException("cacheItem"); } this._source = source; this._reason = reason; this._cacheItem = cacheItem; }
public CacheEntryRemovedArguments(ObjectCache source, CacheEntryRemovedReason reason, System.Runtime.Caching.CacheItem cacheItem) { if (source == null) { throw new ArgumentNullException("source"); } if (cacheItem == null) { throw new ArgumentNullException("cacheItem"); } this._source = source; this._reason = reason; this._cacheItem = cacheItem; }
public void AddOrReplace(string key, object value, TimeSpan timeout) { Ensure.StringArgumentNotNullAndNotEmpty(key, nameof(key)); Ensure.ArgumentNotNull(value, nameof(value)); System.Runtime.Caching.CacheItem item = new System.Runtime.Caching.CacheItem(key, value); System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy(); policy.SlidingExpiration = timeout; // Set non-removable flag to prevent cache item removals due to other reasonse apart from the timeout. e.g. low memory policy.Priority = System.Runtime.Caching.CacheItemPriority.NotRemovable; policy.RemovedCallback = x => OnCacheItemRemoved(new CacheItemRemovedEventArgs(x.CacheItem.Key, x.CacheItem.Value)); cache.Set(item, policy); }
void ICache.Set(string key, CacheInfo cache) { var cacheItem = new System.Runtime.Caching.CacheItem(key, cache.Value); var policy = new System.Runtime.Caching.CacheItemPolicy(); if (cache.AbsoluteExpiration.Ticks > 0) { policy.AbsoluteExpiration = cache.AbsoluteExpiration; } if (cache.SlidingExpiration.Ticks > 0) { policy.SlidingExpiration = cache.SlidingExpiration; } _cache.Set(cacheItem, policy); }
/// <summary> /// 添加缓存 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expiry">过期时间</param> /// <returns></returns> public bool Add(string key, object value, TimeSpan?expiry = null) { #if net40 var cachePolicy = new System.Runtime.Caching.CacheItemPolicy(); if (expiry.HasValue) { cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.Add(expiry.Value); } var item = new System.Runtime.Caching.CacheItem(key, value); Cache.Set(item, cachePolicy); #endif #if netstandard2_0 using (var entry = Cache.CreateEntry(key)) { entry.Value = value; entry.AbsoluteExpirationRelativeToNow = expiry; } #endif return(true); }
/// <summary> /// 获取数据缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="CacheKey">缓存名称</param> /// <param name="cachePopulate"> /// lamda表达式:可以是() => new BLL.CangFangList().Single(CangFangId)或者() => "abc" /// 或者委托(注意返回): delegate() { return new BLL.CangFangList().Single(CangFangId); } /// </param> /// <param name="slidingExpiration">用于设置可调过期时间,它表示当离最后访问超过某个时间段后就过期new TimeSpan(0, 10, 0)或者TimeSpan.FromMinutes(60)</param> /// <remarks> /// var value = CoreHelper.CacheHelper.GetCache<List<Model.S_MenuList>> /// ( /// string.Format("menu{0}", currentUser.Id) /// ,()=>new BLL.S_MenuList().Fetch() /// , TimeSpan.FromMinutes(60) /// ); /// </remarks> /// <returns></returns> public static T GetCache <T>(string CacheKey, Func <T> cachePopulate, TimeSpan slidingExpiration) { if (String.IsNullOrWhiteSpace(CacheKey)) { throw new ArgumentException("无效的缓存键"); } if (cachePopulate == null) { throw new ArgumentNullException("缓存填充"); } System.Web.Caching.Cache objCache = HttpRuntime.Cache; object objObject = objCache[CacheKey]; if (objObject == null) { lock (_locker) { var item = new System.Runtime.Caching.CacheItem(CacheKey, cachePopulate()); SetCache(CacheKey, item.Value, slidingExpiration); } } return((T)objCache[CacheKey]); }
public override void Set(System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) { }
public override System.Runtime.Caching.CacheItem AddOrGetExisting(System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) { throw null; }
public override bool Add(System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) { throw null; }
public abstract void Set(System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy);
public virtual bool Add(System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy) { throw null; }
public abstract System.Runtime.Caching.CacheItem AddOrGetExisting(System.Runtime.Caching.CacheItem value, System.Runtime.Caching.CacheItemPolicy policy);
public bool DelItem(System.Runtime.Caching.CacheItem item) { return(client.Remove(item.Key)); }
public CacheEntryRemovedArguments(System.Runtime.Caching.ObjectCache source, System.Runtime.Caching.CacheEntryRemovedReason reason, System.Runtime.Caching.CacheItem cacheItem) { }
public bool UpdateItem(System.Runtime.Caching.CacheItem item) { return(client.Store(StoreMode.Replace, item.Key, item.Value)); }