From dbcbae88c944d6ef1cb7e0a82bd69ad28d7a2c58 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Fri, 10 Jun 2016 21:15:57 -0400 Subject: [PATCH] update sample --- README.md | 35 +++++++++++++++++++ .../Controllers/HomeController.cs | 33 ++++++++++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index add1bdb..5cc5ede 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,39 @@ In your startup.cs } +``` + +Then in a controller just ask for an IDistributedCache in the constructor. Since this implements Microsoft's IDistributed cache, it could be easily swapped out for redis or another Distributed cache + +```csharp + +public class HomeController : Controller +{ + private readonly IDistributedCache cacheMechanism; + + public HomeController(IDistributedCache cacheMechanism) + { + this.cacheMechanism = cacheMechanism; + } + public async Task Index() + { + var data = await cacheMechanism.GetAsync("awesomeRecord"); + var result = string.Empty; + if(data != null) + { + result = Encoding.UTF32.GetString(data); + } + return View(result); + + } + + public async Task AddCache() + { + cacheMechanism.SetAsync("awesomeRecord", Encoding.UTF32.GetBytes("Im Awesome")); + ViewData["Message"] = "Your application description page."; + + return RedirectToAction("Index"); + } +} + ``` \ No newline at end of file diff --git a/src/AzureTableStorageCacheSample/Controllers/HomeController.cs b/src/AzureTableStorageCacheSample/Controllers/HomeController.cs index 8c9ccbc..acc36f2 100644 --- a/src/AzureTableStorageCacheSample/Controllers/HomeController.cs +++ b/src/AzureTableStorageCacheSample/Controllers/HomeController.cs @@ -1,16 +1,39 @@ -using System; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Distributed; +using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; namespace AzureTableStorageCacheSample.Controllers { public class HomeController : Controller { - public IActionResult Index() + private readonly IDistributedCache cacheMechanism; + + public HomeController(IDistributedCache cacheMechanism) { - return View(); + this.cacheMechanism = cacheMechanism; + } + + public async Task Index() + { + var data = await cacheMechanism.GetAsync("awesomeRecord"); + var result = string.Empty; + if (data != null) + { + result = Encoding.UTF32.GetString(data); + } + return View(result); + } + + public async Task AddCache() + { + await cacheMechanism.SetAsync("awesomeRecord", Encoding.UTF32.GetBytes("Im Awesome")); + ViewData["Message"] = "Your application description page."; + + return RedirectToAction("Index"); } public IActionResult About() @@ -32,4 +55,4 @@ namespace AzureTableStorageCacheSample.Controllers return View(); } } -} +} \ No newline at end of file