对一些浏览频次多、数据量大的数据,使用缓存会比较好,而对一些浏览频次低,或内容因用户不同的,不太适合使用缓存。
在控制器层面,MVC为我们提供了OutputCacheAttribute特性;在数据层使用缓存,用System.Runtime.Caching是不错的选择。
控制器层面使用OutputCacheAttribute缓存
□ OutputCacheAttribute默认的缓存时间是60秒。
class="alt">[OutputCache(Duration=20, VaryByParam="none")]
public ActionResult Index()
{
ViewBag.Message = DateTime.Now.ToString();
return View();
}
□ 设置缓存位置
缓存的位置通过OutputCacheLocation这个枚举来设置,默认的缓存位置是OutputCacheLocation.Any。OutputCacheLocation其它枚举项包括:Client,Downstream, Server, None, or ServerAndClient。如果我们想把一个与用户有关的信息保存在客户端:
[OutputCache(Duration = 7200, Location = OutputCacheLocation.Client, VaryByParam = "none", NoStore = true)]
public ActionResult Index()
{
ViewBag.Message = "Welcome : " + User.Identity.Name;
return View();
}
数据层缓存,通过System.Runtime.Caching
□ 创建一个叫Demo的数据库,并创建表Vehicle。
□ 从数据库生成一个"ADO.NET实体数据模型"CachingDemo.edmx
同时,自动生成了继承EF的DbContext的类DemoEntities:
并且,在配置文件中自动生成了EF相关内容和连接字符串:
□ 引入System.Runtime.Cache
□ 缓存接口
namespace MvcApplication1.Cache
{
public interface ICacheProvider
{
object Get(string key);
void Set(string key, object data, int cacheTime);
bool IsSet(string key);
void Invalidate(string key);
}
}
□ 缓存实现
using System;
using System.Runtime.Caching;
namespace MvcApplication1.Cache
{
public class DefaultCacheProvider : ICacheProvider
{
private ObjectCache Cache
{
get { return MemoryCache.Default; }
}
public object Get(string key)
{
return Cache[key];
}
public void Set(string key, object data, int cacheTime)
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
}
public bool IsSet(string key)
{
return (Cache[key] != null);
}
public void Invalidate(string key)
{
Cache.Remove(key);
}
}
}
□ Vehicle的Repository接口:
using System.Collections.Generic;
using MvcApplication1.Models;
namespace MvcApplication1.Repository
{
public interface IVehicleRepository
{
void ClearCache();
IEnumerable<Vehicle> GetVehicles();
}
}
□ Vehicle的Repository接口实现:
using System.Collections.Generic;
using System.Linq;
using MvcApplication1.Cache;
using MvcApplication1.Models;
namespace MvcApplication1.Repository
{
public class VehicleRepository : IVehicleRepository
{
protected DemoEntities DataContext { get; private set; }
public ICacheProvider Cache { get; set; }
public VehicleRepository() : this(new DefaultCacheProvider())
{
}
public VehicleRepository(ICacheProvider cacheProvider)
{
this.DataContext = new DemoEntities();
this.Cache = cacheProvider;
}
public void ClearCache()
{
Cache.Invalidate("vehicles");
}
public System.Collections.Generic.IEnumerable<Models.Vehicle> GetVehicles()
{
IEnumerable<Vehicle> vehicles = Cache.Get("vehicles") as IEnumerable<Vehicle>;
if (vehicles == null)
{
vehicles = DataContext.Vehicle.OrderBy(v => v.Id).ToList();
if (vehicles.Any())
{
Cache.Set("vehicles",vehicles,30); //设置缓存的时间为30分钟
}
}
return vehicles;
}
}
}
□ HomeController
using System.Web.Mvc;
using MvcApplication1.Repository;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public IVehicleRepository Repository { get; set; }
public HomeController(IVehicleRepository repository)
{
this.Repository = repository;
}
public HomeController() : this(new VehicleRepository())
{
}
public ActionResult Index()
{
return View(Repository.GetVehicles());
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
Repository.ClearCache();
return RedirectToAction("Index");
}
}
}
□ Home/Index.cshtml
@model IEnumerable<MvcApplication1.Models.Vehicle>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style type="text/css">
table td {
border-collapse: collapse;
border: solid 1px black;
}
</style>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<th>编号</th>
<th>车型</th>
<th>价格</th>
</tr>
@foreach (var vehicle in Model)
{
<tr>
<td>@vehicle.Id.ToString()</td>
<td>@vehicle.Name</td>
<td>@string.Format("{0:c}",vehicle.Price)</td>
</tr>
}
</table>
@using (Html.BeginForm())
{
<input type="submit" value="使缓存失效重新获取数据库数据" id="InvalidButton" name="InvalidButton"/>
}
□ 结果
页面第一次加载:
在数据库改变数据,页面内容不变:
点击按钮使缓存失效,视图显示数据库最新数据:
总结
当在数据层使用System.Runtime.Caching,实际上,所有的缓存操作都围绕MemoryCache.Default返回类型为ObjectCache缓存而进行。
□ 参考资料
※ Understanding Caching in Asp.Net MVC with example
※ DATA CACHING WITH .NET 4.0 AND ASP.NET MVC – PART 1