update sample

This commit is contained in:
Tommy Parnell
2016-06-10 21:15:57 -04:00
parent a8be7587fe
commit dbcbae88c9
2 changed files with 63 additions and 5 deletions

View File

@@ -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<IActionResult> 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<IActionResult> AddCache()
{
cacheMechanism.SetAsync("awesomeRecord", Encoding.UTF32.GetBytes("Im Awesome"));
ViewData["Message"] = "Your application description page.";
return RedirectToAction("Index");
}
}
``` ```

View File

@@ -1,16 +1,39 @@
using System; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace AzureTableStorageCacheSample.Controllers namespace AzureTableStorageCacheSample.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
public IActionResult Index() private readonly IDistributedCache cacheMechanism;
public HomeController(IDistributedCache cacheMechanism)
{ {
return View(); this.cacheMechanism = cacheMechanism;
}
public async Task<IActionResult> 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<IActionResult> AddCache()
{
await cacheMechanism.SetAsync("awesomeRecord", Encoding.UTF32.GetBytes("Im Awesome"));
ViewData["Message"] = "Your application description page.";
return RedirectToAction("Index");
} }
public IActionResult About() public IActionResult About()
@@ -32,4 +55,4 @@ namespace AzureTableStorageCacheSample.Controllers
return View(); return View();
} }
} }
} }