如何在 ASP.Net Core 使用 内存缓存

ASP.NET Core 是一个轻量级,模块化的框架,常用来在 Windows,Linux 和 MacOS 上构建高性能,现代化的web框架,不像过去的 Asp.NET,在 ASP.NET Core 中并没有内置 Cache 对象,不过你可以通过 nuget 上的扩展实现如下三种 cache:

  • in-memory caching
  • distributed caching
  • response caching

在本文中,我们来看看如何将那些不易变的数据灌到内存中实现 ASP.NET Core application 的高性能,然后我会用一些例子来说明这些概念。

如何启用 in-memory cache

要想将 in-memory cache 集成到 ASP.NET Core 中,就需要将其注入到 ServiceCollection 容器,如下代码所示:


public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddMemoryCache();
}

集成好之后,接下来了解一下缓存的统一接口:IMemoryCache ,代码如下:


public interface IMemoryCache : Idisposable
{
    bool TryGetValue(object key, out object value);
    ICacheEntry CreateEntry(object key);
    void Remove(object key);
}

那如何在 Controller 中使用呢?可以使用 Controller 的构造函数实现注入,如下代码所示:


    public class HomeController : Controller
    {
        private readonly ILogger

到现在为止,in-memory caching 的配置全部做完,现在可以考虑如何实现从 Cache 读取和写入了。

Cache的读取和写入

利用 IMemoryCache 接口的 Set 可实现向缓存中写入数据,请注意这个 Set 方法接收两个参数,第一个参数是缓存的名字,第二个参数就是你要缓存的内容,如下代码所示:


        public IActionResult Index()
        {
            cache.Set("IDGKey", DateTime.Now.ToString());
            return View();
        }

从 Cache 中提取内容,需要使用 IMemoryCache 接口的 TryGet() 方法,下面是对 Index 方法一个修改版本,代码如下:


        public IActionResult Index()
        {
            string key = "IDGKey";

            string obj;
            if (!cache.TryGetValue<string>(key, out obj))
            {
                obj = DateTime.Now.ToString();
                cache.Set<string>(key, obj);
            }

            ViewBag.Cache = obj;

            return View();
        }

还有一个叫做 GetorCreate 方法,从名字上就能看出来,如果获取不到就会创建一个,如下代码所示:


        public IActionResult Index()
        {
            cache.GetorCreate<string>("IDGKey", cacheEntry =>
            {
                return DateTime.Now.ToString();
            });

            return View();
        }

对了,除了同步版本的 GetorCreate,还有一个支持异步的 GetorCreateAsync

Cache 的过期策略

可以对缓存数据指定过期策略,比如说:绝对过期时间 和 滑动过期时间,前者表示缓存数据的绝对存活时间,时间一到就会立即移除,后者表示指定的时间间隔内数据没有被访问到,那么就会被移除,如果不明白的化,参考 Session 的过期机制。

要想设置过期策略,可以通过 MemoryCacheEntryOptions 类来配置,如下代码所示:


        public IActionResult Index()
        {
            MemoryCacheEntryOptions cacheExpirationoptions = new MemoryCacheEntryOptions();

            cacheExpirationoptions.AbsoluteExpiration = DateTime.Now.AddMinutes(30);

            cacheExpirationoptions.Priority = CacheItemPriority.normal;

            cache.Set<string>("IDGKey", DateTime.Now.ToString(), cacheExpirationoptions);

            return View();
        }

值得注意的是上面的 Priority 属性,它的应用场景是这样的,当应用程序内存不够时要回收内存的过程中,谁的优先级低就会被优先移除,除了normal 枚举,还有其他诸如:Low, High, NeverRemove ,除了 NeverRemove ,其他的几种都会被回收机制管控。

新的 Cache 机制还提供了一个

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。