其他分享
首页 > 其他分享> > Cache缓存-帮助类(MemoryCache)

Cache缓存-帮助类(MemoryCache)

作者:互联网

 public interface ICache
    {
        /// <summary>
        /// 添加缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">要缓存的对象</param>
        void Add(string key, object obj);
        /// <summary>
        /// 添加有时间限制的缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">要缓存的对象</param>
        /// <param name="seconds">缓存时间</param>
        void Add(string key, object obj, int seconds);
        /// <summary>
        /// 判断指定的Key是否已经有缓存数据
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>true/false</returns>
        bool Exists(string key);
        /// <summary>
        /// 获取缓存的对象
        /// </summary>
        /// <typeparam name="T">缓存对象的类型</typeparam>
        /// <param name="key">缓存Key</param>
        /// <returns>缓存的对象</returns>
        T Get<T>(string key);
        /// <summary>
        /// 移除缓存项
        /// </summary>
        /// <param name="key">缓存Key</param>
        void Remove(string key);
    }
 public class Cache : ICache
    {
        private readonly MemoryCache _cache;
        private readonly CacheItemPolicy _defaultCacheItemPolicy;
        public Cache()
        {
            _cache = MemoryCache.Default;
            _defaultCacheItemPolicy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromSeconds(60 * 2) };
        }
        /// <summary>
        /// 添加缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">要缓存的对象</param>
        public void Add(string key, object obj)
        {
            var cacheItem = new CacheItem(key, obj);
            _cache.Set(cacheItem, _defaultCacheItemPolicy);
        }
        /// <summary>
        /// 添加有时间限制的缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">要缓存的对象</param>
        /// <param name="seconds">缓存时间 默认7200</param>
        public void Add(string key, object obj, int seconds = 7200)
        {
            _cache.Set(key, obj, DateTimeOffset.Now.AddSeconds(seconds));
        }
        /// <summary>
        /// 判断指定的Key是否已经有缓存数据
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>true/false</returns>
        public bool Exists(string key)
        {
            return _cache.Get(key) != null;
        }
        /// <summary>
        /// 获取缓存的对象
        /// </summary>
        /// <typeparam name="T">缓存对象的类型</typeparam>
        /// <param name="key">缓存Key</param>
        /// <returns>缓存的对象</returns>
        public T Get<T>(string key)
        {
            return (T)_cache.Get(key);
        }
        /// <summary>
        /// 移除缓存项
        /// </summary>
        /// <param name="key">缓存Key</param>
        public void Remove(string key)
        {
            _cache.Remove(key);
        }
    }

 --

// 数据缓存,但对于需要实时数据来说,这里不能加的。

public HomeModule(IUserMapper userMapper) //: base("/home")
{
Get("/", _ =>
{
// 数据缓存,但对于需要实时数据来说,这里不能加的。
string time = DateTime.Now.ToString();
ICache cache = new Cache();
if (!cache.Exists("time"))
{
cache.Add("time", time, 10);
return DateTime.Now.ToString();
}
return cache.Get<string>("time");
//Get("/", _=> { return View["list"]; });// "Hello World!");
});

}

标签:缓存,Key,MemoryCache,Cache,cache,key,public,string
来源: https://www.cnblogs.com/Insist-Y/p/15262775.html