Azure Redis Cache is based on the popular open-source Redis cache. It gives you access to a secure, dedicated Redis cache, managed by Microsoft and accessible from any application within Azure.
Features
Azure Redis Cache is available in the following tiers:
Additional features include:
Create a Redis Cache using the Azure Management portal
In the Azure Management portal you can create a Redis Cache by selecting Redis Cache-> Create as shown in Figure1
Next you will need to first select a name (DNS name) that is unique. The resource group your cache will belong to, and the location. You should strive to select a location that is in the same region as the services that will use the cache for best performance. The DNS name for your cache will be {cache name}.redis.cache.windows.net.
After the cache is created you will need two pieces of information from the Redis Cache blade to start using the cache in your code: the host name and an access key. You can access both of these in the Redis Cache blade as shown in Figure3
Using Azure Redis
Storing Session State in Azure Redis
Sometimes your web application requires state but cookies aren’t sufficient for your needs. Session state provides a server managed state mechanism that can help meet an application’s needs for storing state. Microsoft has provided a way to easily integrate Azure Redis Cache with shared session in .Net applications.
First, you’ll need to install the required Nuget packages into your project. Click on “Manage Nuget Packages” in your project and search for RedisSessionStateProvider. You should see Microsoft.Web.RedisSessionStateProvider by Microsoft. Click on that one and install it. Next you’ll need to configure the applications shared session state to use the Azure Redis Cache you have created. To do this you’ll need to modify the sessionState portion of the Web.config.
Once you have the config done, you’re ready to start utilizing the cache as normal. An example controller is shown below:
A few notes about using Azure Redis Cache for Session State:
Storing Html Output Cache in Azure Redis
The output cache is a great way to improve your sites performance by storing html in a cache to reduce response times and load for your site. Microsoft has also provided a way to easily configure your .Net application to use Azure Redis Cache for an Html output cache.
The first step is to add the Nuget package to your project. Click on “Manage Nuget Packages” in your project and search for RedisOutputCacheProvider. You should see Microsoft.Web.RedisOutputCacheProvider by Microsoft. Click on that one and install it. Next is to configure the output cache to use the Azure Redis Cache by altering the caching section in the Web.config.
Now you can start defining what you want to store in the output cache. You can do this in the controller or in a view. You can simply specify a controller action result to store in the output cache by adding the OutputCache attribute to the action method.
You can also specify in the view by doing
This very simple code will store the pages in the cache for the specified amount of time. You can also configure the cache to vary by headers, parameters, etc.
Custom use of the Azure Redis Cache
To configure the Azure Redis Cache for general use, simply install the StackExchange.Redis by Stack Exchange Nuget package. Once installed, you can connect and interact with the cache easily. Below is a sample controller which utilizes a connection to the cache to increment a value. That value is also retrieved and displayed on a different view utilizing the same connection property.
References
https://azure.microsoft.com
Du musst angemeldet sein, um einen Kommentar abzugeben.