一、MVC缓存简介

缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期。在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法。
MVC缓存本质上还是.NET的一套缓存体系,只不过该缓存体系应用在了MVC框架上。下面的示例把缓存应用在MVC上。

缓存的常用场景:

数据被频繁的使用,并且很少发生变化或对即时性的要求不高。

二、Control缓存

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MvcCache.Control.Controllers
 8 {
 9     [OutputCache(Duration = 10)]
10     public class ControlController : Controller
11     {
12         //
13         // GET: /Home/
14         public ActionResult Index()
15         {
16             ViewBag.CurrentTime = System.DateTime.Now;
17             return View();
18         }
19 
20         public ActionResult Index1()
21         {
22             ViewBag.CurrentTime = System.DateTime.Now;
23             return View();
24         }
25 
26     }
27 }
View Code

相关文章:

  • 2021-12-05
  • 2021-11-23
  • 2021-05-05
  • 2021-12-11
  • 2021-10-25
  • 2021-09-26
  • 2021-07-28
猜你喜欢
  • 2021-06-07
  • 2021-12-20
  • 2022-12-23
相关资源
相似解决方案