MVC使用自定义缓存:首先我是在一个工具类库中新建一个缓存帮助类,比如这里我在Itcast.CMS.Common 类库中新建了一个CacheHelper.cs类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Itcast.CMS.Common
{
/// <summary>
/// 缓存辅助类
/// </summary>
public class CacheHelper
{
//在类库中使用Caching,或者HttpRuntime都需要添加System.Web引用
//private static System.Web.Caching.Cache Cache = System.Web.HttpContext.Current.Cache;
//也可以用下面这句代码代替上面这句代码
private static System.Web.Caching.Cache Cache = HttpRuntime.Cache;
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <returns></returns>
public static object SetCache(string key, object val)
{
if (Cache[key] == null)
Cache.Insert(key, val, null, DateTime.Now.AddDays(1), TimeSpan.Zero);
return Cache[key];
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="minute">缓存的时间长度(单位:分钟)</param>
/// <returns></returns>
public static object SetCache(string key, object val, double minute)
{
if (Cache[key] == null)
Cache.Insert(key, val, null, DateTime.Now.AddMinutes(minute), TimeSpan.Zero);
return Cache[key];
}
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static object GetCache(string key)
{
return Cache[key];
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
/// <param name="key">要移除缓存的键</param>
public static void RemoveCache(string key)
{
if (Cache[key] != null)
Cache.Remove(key);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
//Cache.GetEnumerator()方法是检索用于循环访问包含在缓存中的键设置及其值的字典枚举数。
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
// MoveNext()方法是将枚举数推进到集合的下一个元素。
while (CacheEnum.MoveNext())
{
Cache.Remove(CacheEnum.Key.ToString());
}
}
}
}
在项目中的控制器的方法里直接使用缓存
HomeController.cs控制器
using Itcast.CMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Itcast.CMS.WebApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
Itcast.CMS.BLL.T_UserInfoService userinfo = new BLL.T_UserInfoService();
//如果key为“T_User_list”的这个缓存为空,我们就去数据库获取数据
if (Itcast.CMS.Common.CacheHelper.GetCache("T_User_list") == null)
{
//从BLL层中要数据(BLL层又会去DAL层要数据,DAL会调用数据库获取数据)。
List<T_UserInfo> list = userinfo.GetUserInfo();
//将要过来的数据存入key为“T_User_list”的这个缓存中,设置过期时间为1分钟
Itcast.CMS.Common.CacheHelper.SetCache("T_User_list", list, 1);
return View(list);
}
else
{
//如果如果key为“T_User_list”的这个缓存里存在数据,那么就将缓存里的数据给Model
return View(Itcast.CMS.Common.CacheHelper.GetCache("T_User_list"));
}
}
}
}
当用户第一次访问我的时候,缓存是不存在的,那么就从数据库里查询数据,并数据保存到缓存里, 当用户第二次来访问我的时候,因为缓存已经存在了(前提,没有超过我们设置的过期时间)那么就直接从缓存里取得数据,给用户,这样就不必请求数据库了。
Home控制器下的Index视图
@model IEnumerable< Itcast.CMS.Model.T_UserInfo>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<tr><th>编号</th><th>网名</th><th>姓名</th><th>年龄</th><th>性别</th><th>电话</th><th>邮箱</th><th>地址</th><th>备注</th></tr>
@foreach (var v in Model)
{
<tr><td>@v.Id</td><td>@v.UserName</td><td>@v.Name</td><td>@v.Age</td><td>@v.Gender</td><td>@v.Mobile</td><td>@v.Email</td><td>@v.Addres</td><td>@v.Remarks</td><td>@v.Id</td></tr>
}
</table>
</div>
</body>
</html>
项目组成图: