/// <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,
     });
 }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <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());
            }
        }
示例#4
0
        /// <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");
            }
        }
示例#5
0
        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));
        }
示例#6
0
        /// <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));
 }
示例#8
0
        async Task IAsyncCacheProvider.Set(string cacheKey, object value, CachePolicyItem cachePolicy)
        {
            var provider = await GetCacheProvider(cacheKey);

            await provider.Set(cacheKey, value, cachePolicy);
        }
示例#9
0
 public Task Set(string key, object value, CachePolicyItem cachePolicy)
 {
     provider.Set(key, value, cachePolicy);
     return(Task.FromResult((object)null));
 }
示例#10
0
 public SpecifyCachePolicy(CachePolicyItem item)
 {
     _item = item;
 }
示例#11
0
 void ICacheProvider.Set(string cacheKey, object value, CachePolicyItem cachePolicy)
 {
 }