Files
AzureTableStorageCache/README.md
Tommy Parnell 2511d20fea fix example
2016-06-10 21:17:07 -04:00

1.9 KiB

AspNetCache-AzureTableStorage

Use azure table storage for AspNet core 1.0 Distributed Cache.

Azure Table Storage is a very cheap, super fast key value store, and its much cheaper than the redis cluster in azure. This is not a true replacement for redis, and redis should be used if people have money, but this is designed to get people a very cheap cache in azure. Currently this doesn't actually support the dotnet core runtime, and won't until the Azure Storage client is updated to support core.

How to use

install-package AzureTableStorageCache

In your startup.cs


 public void ConfigureServices(IServiceCollection services)
        {
            services.AddAzureTableStorageCache("!!!CONNECTIONSTRINGHERE!!!", "tablename", "partitionKey");
            // Add framework services.
            services.AddMvc();
        }


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


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()
        {
            await cacheMechanism.SetAsync("awesomeRecord", Encoding.UTF32.GetBytes("Im Awesome"));
            ViewData["Message"] = "Your application description page.";

            return RedirectToAction("Index");
        }
}