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