/// <summary> /// 根据现有缓存策略创建 MemoryCache 的缓存策略 /// </summary> /// <param name="cachePolicy">缓存策略</param> /// <returns>MemoryCache 的缓存策略</returns> private CacheItemPolicy CreateCacheItemPolicy(CachePolicyItem cachePolicy) { return(new CacheItemPolicy { AbsoluteExpiration = cachePolicy.Expires, Priority = cachePolicy.Priority.IsHighPriority ? CacheItemPriority.NotRemovable : CacheItemPriority.Default, }); }
/// <summary> /// 设置一个缓存 /// </summary> /// <param name="cacheKey">缓存键</param> /// <param name="value">缓存值</param> /// <param name="cachePolicy">缓存策略</param> public async Task Set(string cacheKey, object value, CachePolicyItem cachePolicy) { _manager.SetCachePolicy(cacheKey, cachePolicy); var stream = new MemoryStream(); _serializer.Serialize(stream, value); await _manager.WriteStream(cacheKey, stream); }
/// <summary> /// 设置缓存策略对象 /// </summary> /// <param name="cacheKey"></param> /// <param name="cachePolicy"></param> public void SetCachePolicy(string cacheKey, CachePolicyItem cachePolicy) { var filepath = Path.Combine(CurrentDirectory, cacheKey + ".policy"); using (var writer = new StreamWriter(new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Delete), Encoding.UTF8)) { writer.Write(cachePolicy.ToString()); } }
/// <summary> /// 创建一个缓存项 /// </summary> /// <param name="value">缓存值</param> /// <param name="policy">缓存策略</param> public CacheEntry(T value, CachePolicyItem policy) { _value = value; _policy = policy; if (_policy == null) { throw new ArgumentNullException("policy"); } }
public virtual async Task Set(string key, object value, CachePolicyItem cachePolicy) { //清除一级缓存,写入二级缓存 await L1Cache.Remove(key); await L2Cache.Set(key, value, cachePolicy); //后台写入一级缓存。 Background(L1Cache.Set(key, value, cachePolicy)); }
/// <summary> /// 获取缓存策略对象 /// </summary> /// <param name="cacheKey">缓存键</param> /// <returns>缓存策略对象</returns> public CachePolicyItem GetCachePolicy(string cacheKey) { var filepath = Path.Combine(CurrentDirectory, cacheKey + ".policy"); if (File.Exists(filepath) == false) { return(CachePolicyItem.InvalidCachePolicy); } try { string data; using (var reader = new StreamReader(new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite), Encoding.UTF8)) { data = reader.ReadToEnd(); } return(CachePolicyItem.Parse(data)); } catch (IOException) { return(CachePolicyItem.InvalidCachePolicy); } }
/// <summary> /// 设置缓存值 /// </summary> /// <param name="key">缓存键</param> /// <param name="value">缓存值</param> /// <param name="cachePolicy">缓存策略</param> public void Set(string key, object value, CachePolicyItem cachePolicy) { _host.Set(new System.Runtime.Caching.CacheItem(key, value), CreateCacheItemPolicy(cachePolicy)); }
async Task IAsyncCacheProvider.Set(string cacheKey, object value, CachePolicyItem cachePolicy) { var provider = await GetCacheProvider(cacheKey); await provider.Set(cacheKey, value, cachePolicy); }
public Task Set(string key, object value, CachePolicyItem cachePolicy) { provider.Set(key, value, cachePolicy); return(Task.FromResult((object)null)); }
public SpecifyCachePolicy(CachePolicyItem item) { _item = item; }
void ICacheProvider.Set(string cacheKey, object value, CachePolicyItem cachePolicy) { }