浅谈MVC缓存

缓存是将信息放在内存中以避免频繁访问数据库从数据库中提取数据,在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法。

对于MVC有Control缓存和Action缓存。

一、Control缓存

Control缓存即是把缓存应用到整个Control上,该Control下的所有Action都会被缓存起来。

我们来看一下例子:

  [OutputCache(Duration = 10)]
    public class HomeController : Controller
    {
      
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.CurrentTime = DateTime.Now;
            return View();
        }
    }

view中:

@{
    ViewBag.Title = "Index";
}

<h2>@ViewBag.CurrentTime</h2>

效果:

不停的刷新页面,时间会每10秒钟更新一次。

二、Action缓存

将缓存加载Action上,这样,只有加缓存的Action才会有缓存,其他的Action没有。

写法与Control的缓存一样。

 //Action缓存,10秒
        [OutputCache(Duration = 10)]
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.CurrentTime = DateTime.Now;
            return View();
        }

        public ActionResult Index2()
        {
            ViewBag.CurrentTime = DateTime.Now;
            return View();
        }

这里分别有两个Action,一个加了缓存的Index,还有一个没有加缓存的Index2

效果:

分别访问这两个页面,不停的刷新这两个页面,Index 中的时间会10秒钟更新一次,而Index2中会实时更新。

三、使用配置文件进行缓存配置

在MVC的Web.config文件中,可以对缓存进行相关的配置。

在system.web节点中,添加caching子节点,然后如下:

 <outputCacheSettings>
        <outputCacheProfiles>
          <add name="TestConfigCache" duration="10" />
        </outputCacheProfiles>
 </outputCacheSettings>

配置好后,我们的Control缓存或者Action缓存就可以这么写:

        [OutputCache(CacheProfile= "TestConfigCache")]
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.CurrentTime = DateTime.Now;
            return View();
        }

四、缓存依赖

 缓存数据是从数据库中某个表获取的,如果数据库中对应的表的数据没有发生改变,我们就没有必要对缓存数据进行更新,如果数据库对应的表的数据发生了变化,那么,我们相应的缓存数据就应立即更新。

那么缓存是否过期,依赖于数据库对应的表中的数据是否发生变化。这就是缓存依赖。下面我们来写一下。

我们MVC的Web.config文件中进行如下未配置:

1、首先配置数据库连接字符串:

 <connectionStrings>
    <add name="sqlCon" connectionString="server=127.0.0.1;database=test;uid=sa;pwd=123456" providerName="System.Data.SqlClient" />
  </connectionStrings>

2、进行缓存依赖配置:

    <caching>
      <sqlCacheDependency>
        <databases>
          <add name="PersonCacheDependency" connectionStringName="sqlCon" pollTime="500"/>
        </databases>
      </sqlCacheDependency>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="TestConfigCache" duration="3600" sqlDependency="PersonCacheDependency:Person"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

其中pollTime为监听数据库变化的间隔时间(毫秒)

以上配置说明:库名:test,监听表名:Person。缓存时间为:3600秒即一小时。数据库依赖周期为500毫秒,即每0.5秒监听下数据库是否有变化,如果有变化则立即更新缓存。

Control中或Action中:

        [OutputCache(CacheProfile= "TestConfigCache")]
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.CurrentTime = DateTime.Now;
            return View();
        }

这样,在一个小时内,只有Person表中的数据发生变化后,缓存才会更新,不然缓存不会更新。

五、注:

当我们配置完缓存以来后,运行我们的项目,可能会出现一下错误提示:

这是因为我们没有对Person表启用缓存通知。

打开vs命令工具行,输入:aspnet_regsql -S localhost -U sa -P 123456 -ed -d test-et -t Person

这样就可以解决上述错误。

好了,MVC的缓存就介绍到这里。见识浅薄,有不当之处,还望大神们指点一二。


 

posted @ 2017-03-27 15:07  萌萌丶小魔王  阅读(1935)  评论(5编辑  收藏  举报