.NET CORE下的Cache

.NET CORE 下的缓存跟之前ASP.NET下的缓存有所不同,应用.NET CORE缓存首先需要引入Microsoft.Extensions.Caching.Memory程序包

下面简单写了一个CacheHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Extensions.Caching.Memory;

namespace Common
{
    public class CacheHelper
    {
        private static MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
        /// 
        /// 添加缓存
        /// 
        /// 
        /// 
        /// 缓存有效时间
        public static void Add(string key, object value, int minutes)
        {            
            memoryCache.Set(key, value, new TimeSpan(0, minutes, 0));            
        }

        /// 
        /// 获取缓存值
        /// 
        /// 
        /// 
        public static object GetValue(string key)
        {
            object value = null;
            memoryCache.TryGetValue(key, out value);
            return value;
        }


        /// 
        /// 删除缓存
        /// 
        /// 
        public static void Remove(string key)
        {
            memoryCache.Remove(key);
        }
    }
}

 

你可能感兴趣的:(.NET CORE下的Cache)