How can I use System.Web.Caching.Cache in a Console application?

后端 未结 6 587
死守一世寂寞
死守一世寂寞 2021-02-01 03:47

Context: .Net 3.5, C#
I\'d like to have caching mechanism in my Console application.
Instead of re-inventing the wheel, I\'d like to use System.Web.Caching.Cache

6条回答
  •  清歌不尽
    2021-02-01 04:17

    Try

    public class AspnetDataCache : IDataCache
    {
        private readonly Cache _cache;
    
        public AspnetDataCache(Cache cache)
        {
            _cache = cache;
        }
    
        public AspnetDataCache()
            : this(HttpRuntime.Cache)
        {
    
        }
        public void Put(string key, object obj, TimeSpan expireNext)
        {
            if (key == null || obj == null)
                return;
            _cache.Insert(key, obj, null, DateTime.Now.Add(expireNext), TimeSpan.Zero);
        }
    
        public object Get(string key)
        {
            return _cache.Get(key);
        }
    
    

提交回复
热议问题