MVC 使用自訂緩衝,mvc自訂緩衝
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>
項目組成圖: