Asp.Net Core 缓存的使用(译)
原文:http://www.binaryintellect.net/articles/a7d9edfd-1f86-45f8-a668-64cc86d8e248.aspx
環境:Visual Studio 2017, Asp.Net Core 1.1.
緩存機制主要是為了提高性能。在ASP.NET Web Forms 及 ASP.NET MVC中可以直接使用緩存對象(Caching object)緩存數據。這通常被稱作“服務器端緩存”,是 Framework 的內置特性。雖然Asp.Net Core沒有這樣的緩存對象,但依然可以輕松的實現緩存功能。本文將對此進行介紹。
繼續閱讀之前,請先使用 Web Application 項目模板創建一個 Asp.net Core 的應用程序:
| 注:使用緩存功能,需通過NuGet安裝“Microsoft.Extensions.Caching.Memory”擴展包。使用時引用:using Microsoft.Extensions.Caching.Memory; |
1. 在Startup類中開啟緩存功能
如前所述,Asp.Net Core 沒有內建的緩存對象,因此不能在 Controller 中使用它們。在Asp.Net Core中要使用內存緩存,需要在 Startup 類中依賴注入(DI)內存緩存服務(in-memory caching service)。打開Startup 文件,修改 ConfigureServices() 方法如下:
public void ConfigureServices(IServiceCollection services) {services.AddMvc();services.AddMemoryCache(); }?AddMemoryCache() 方法將內存緩存功能加入到服務集合中。
2. 控制器中注入緩存對象
修改HomeController如下:
public class HomeController : Controller {private IMemoryCache cache;public HomeController(IMemoryCache cache){this.cache = cache;}... }通過構造函數注入緩存對象,我們在控制器中就有了可用的緩存變量。
3. 使用Set()方法緩存一個對象
?有了IMemoryCache 對象,你就可以非常方便讀取或寫入緩存數據。
public IActionResult Index() {cache.Set<string>("timestamp", DateTime.Now.ToString());return View(); }上面代碼使用 IMemoryCache 的 Set<T>() 方法將一條記錄加入到了緩存中。Set() 方法第一個參數為緩存名稱,第二個參數為其值。
4. 使用Get()方法獲取緩存
?一旦您添加了緩存,就可以通過 Get() 方法來獲取它們。
public IActionResult Show() {string timestamp = cache.Get<string>("timestamp");return View("Show", timestamp); }上面代碼演示了從緩存中獲得數據,Get() 方法指定了緩存名和返回數據類型。
下面用于顯示獲得的緩存:
<h1>TimeStamp : @Model</h1> <h2>@Html.ActionLink("Go back", "Index", "Home")</h2>運行你的應用程序,打開 /Home/Index ,此時將生成了一個時間戳并存入緩存中。然后打開 /Home/Show 觀察是否已從緩存中獲得了時間戳數據。/Home/Show 頁面會顯示如下:
5. 使用TryGet()方法檢查是否存在指定緩存項
上面示列中,每次訪問 /Home/Index 都將生成一個新的時間戳并進行緩存,這是因為沒有檢查是否已存在同名的緩存項。有兩種方式檢測同名緩存項:
//first way if (string.IsNullOrEmpty(cache.Get<string>("timestamp"))) {cache.Set<string>("timestamp", DateTime.Now.ToString()); }//second way if (!cache.TryGetValue<string>("timestamp", out string timestamp)) {cache.Set<string>("timestamp", DateTime.Now.ToString()); }第一種方法仍然使用Get()方法,如未能獲取到值,則返回 IsNullOrEmpty()。
第二種方法更加優雅一些,使用TryGet()方法獲取值。TryGet()方法返回一個布爾值來表示緩存項是否存在,實際的緩存值將通過 out 參數獲得。
6. 使用GetOrCreate()添加不存在的緩存項
你可能有這樣的需求,緩存項如果存在則取出,如果不存在則創建。此時可以使用 GetOrCreate() 方法:
public IActionResult Show() {string timestamp = cache.GetOrCreate<string>("timestamp", entry => {return DateTime.Now.ToString();});return View("Show", timestamp); }使用GetOrCreate() 方法檢查timestamp是否存在,如果存在則直接賦值給本地變量,反之則創建一個新的緩存項。
現在直接訪問/Home/Show頁面,可以看到timestamp已顯示。timestamp并沒有通過/Home/Index賦值。
7. 設置緩存的決絕和滑動(sliding)期限
在之前的示列中,緩存數據會一直存在,直到調用Remove()方法。你也可以為緩存設置絕對期限和滑動期限。絕對期限就是為緩存設置一個明確的過期時間,滑動期限就是空閑多少時間之后移除緩存。
你可以通過 MemoryCacheEntryOptions對象設置緩存期限策略:
public IActionResult Index() {//cache.Set<string>("timestamp", DateTime.Now.ToString()); MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();options.AbsoluteExpiration = DateTime.Now.AddMinutes(1);options.SlidingExpiration = TimeSpan.FromMinutes(1);cache.Set<string>("timestamp", DateTime.Now.ToString(), options);return View(); }上面代碼在Index() action中創建了MemoryCacheEntryOptions對象,并同時設置了一個絕對期限為1分鐘與滑動期限為1分鐘的策略。MemoryCacheEntryOptions對象作為Set()方法的第三個參數傳遞。
8. 定義移除緩存之后的回調函數
有時候你可能需要在緩存移除后獲得通知。緩存被移除有以下2種原因,一是調用了Remove()方法,二是到期后自動移除。你可以通過設定回調函數獲得移除緩存之后的通知:
public IActionResult Index() {//cache.Set<string>("timestamp", DateTime.Now.ToString()); MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();options.AbsoluteExpiration = DateTime.Now.AddMinutes(1); options.SlidingExpiration = TimeSpan.FromMinutes(1); options.RegisterPostEvictionCallback(MyCallback, this);cache.Set<string>("timestamp", DateTime.Now.ToString(), options);return View(); }?上面代碼示例了通過RegisterPostEvictionCallback方法設定回調函數,在本例中回調函數名為MyCallback,第二個參數是你希望傳遞給回調函數的狀態參數,本例我們將HomeController實例作為狀態參數傳遞給回調函數。
回調函數方法如下:
private static void MyCallback(object key, object value, EvictionReason reason, object state) {var message = $"Cache entry was removed : {reason}";((HomeController)state).cache.Set("callbackMessage", message); }看看上面代碼,MyCallback() 是一個在HomeController中的私有靜態函數,有四個參數。前兩個參數是被移除的緩存數據所對應的鍵/值,第三個參數表示移除的原因。EvictionReason是一個枚舉類型,可能原因有過期、移除和替換。
在函數體中,我們僅定義了一個string類型的message表示移除原因。如果我們想要把這個message作為另一個緩存項,就需要訪問HomeController的緩存對象,此時就要使用第四個參數state。使用state對象獲得HomeController中的緩存,同時使用Set()方法設置了一個名為“callbackMessage”的緩存項。
callbackMessage可以在Show() action中獲得:
public IActionResult Show() {string timestamp = cache.Get<string>("timestamp");ViewData["callbackMessage"] = cache.Get<string>("callbackMessage");return View("Show", timestamp); }在Show頁面呈現:
<h1>TimeStamp : @Model</h1> <h3>@ViewData["callbackMessage"]</h3> <h2>@Html.ActionLink("Go back", "Index", "Home")</h2>測試回調方法,運行程序并訪問/Home/Index頁面,然后再訪問/Home/Show頁面,不斷刷新Show頁面直到緩存過期,頁面會顯示過期原因。
9. 緩存的優先級
和過期策略一樣,緩存優先級同樣使用MemoryCacheEntryOptions:
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions(); options.Priority = CacheItemPriority.Normal; cache.Set<string>("timestamp", DateTime.Now.ToString(), options);CacheItemPriority枚舉值有普通,高和永久保留。
10. 設置緩存之間的依賴關系
可以為緩存數據設置依賴關系,實現刪除一個緩存項后,其依賴的緩存項同時被移除的功能。請按如下代碼更新Index() action:
public IActionResult Index() {var cts = new CancellationTokenSource();cache.Set("cts", cts);MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();options.AddExpirationToken(new CancellationChangeToken(cts.Token));options.RegisterPostEvictionCallback(MyCallback, this);cache.Set<string>("timestamp",DateTime.Now.ToString(), options);cache.Set<string>("key1", "Hello World!",new CancellationChangeToken(cts.Token));cache.Set<string>("key2", "Hello Universe!",new CancellationChangeToken(cts.Token));return View(); }| 注:通過NuGet安裝“Microsoft.Extensions.Primitives”程序包,同時添加對System.Threading的引用。 |
上面代碼首先創建了一個CancellationTokenSource對象,并將其設置成名為“cts”的緩存項。然后創建MemoryCacheEntryOptions對象,并使用AddExpirationToken()方法指定一個特殊的過期令牌(expiration token),這里我們暫不深究CancellationChangeToken。
expiration token允許你“過期”一個緩存項,如果令牌是“active”狀態則保留緩存項,如果是“cancelled”則從緩存中移除。一旦緩存項被移除則調用MyCallback方法。代碼接著創建了兩個緩存項 key1和key2,創建時第三參數傳遞了一個使用cts對象初始化的CancellationChangeToken。
現在我們有了三個緩存項,timestamp是主緩存項,key1和key2依賴與timestamp。當timestamp被移除時,會同時移除key1和key2。移除timestamp需要取消它的令牌。
public IActionResult Remove() {CancellationTokenSource cts = cache.Get<CancellationTokenSource>("cts");cts.Cancel();return RedirectToAction("Show"); }我們從緩存中獲得先前定義的CancellationTokenSource對象同時執行Cancel()方法,timestamp移除的同時key1與key2也一并移除了。
測試一下上面代碼,運行程序并訪問/Home/Index頁面,再訪問/Home/Show頁面,同時檢查三個緩存項是否按預期顯示。然后訪問/Home/Remove,你將重新被導航到/Home/Show頁面。當Remove() action被調用時,令牌被標記為取消,所有的值將被移除。Show頁面將顯示過期原因為“令牌過期”:
?
轉載于:https://www.cnblogs.com/lookerblue/p/7077523.html
總結
以上是生活随笔為你收集整理的Asp.Net Core 缓存的使用(译)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 下安装redis
- 下一篇: 1063. Set Similarity