{"id":22021,"date":"2013-11-30T14:32:45","date_gmt":"2013-11-30T21:32:45","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/aspnet\/?p=22021"},"modified":"2013-11-30T14:32:45","modified_gmt":"2013-11-30T21:32:45","slug":"instant-azure-caching-with-mvc","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/instant-azure-caching-with-mvc\/","title":{"rendered":"Instant Azure Caching with MVC"},"content":{"rendered":"<p><font color=\"#ff0000\"><strong>Warning. This is obsolete, see my new blog <\/strong><\/font><\/p>\n<h3><a href=\"http:\/\/azure.microsoft.com\/blog\/2014\/06\/05\/mvc-movie-app-with-azure-redis-cache-in-15-minutes\/\" target=\"_blank\" rel=\"noopener\">MVC movie app with Azure Redis Cache in 15 minutes<\/a><\/h3>\n<p><font color=\"#ff0000\"><strong>Warning: Most of the information in this blog is for the Preview edition and should not be used or targeted for production code<\/strong><\/font>. Use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/hh914161.aspx\">In-Role Cache<\/a> for production code.<\/p>\n<p>In this blog post I\u2019ll take my <a href=\"http:\/\/www.asp.net\/mvc\/tutorials\/mvc-5\/introduction\/getting-started\">MVC Movie app<\/a> and in 15 minutes connect it to the preview <a href=\"http:\/\/weblogs.asp.net\/scottgu\/archive\/2013\/09\/03\/windows-azure-new-distributed-dedicated-high-performance-cache-service-more-cool-improvements.aspx\">Windows Azure Cache<\/a> service. The image below shows the details of a movie along with timing data (1.4 milliseconds), cache status (<em>Hit<\/em> in this case) and the PID of the IIS server.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/3581.details.png\"><img decoding=\"async\" title=\"details\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"details\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/0702.details_thumb.png\" width=\"572\" height=\"662\" \/><\/a><\/p>\n<p>You can download the completed app <a href=\"http:\/\/archive.msdn.microsoft.com\/Project\/Download\/FileDownload.aspx?ProjectName=aspnetmvcsamples&amp;DownloadId=16476\">here<\/a>. Later in the tutorial we\u2019ll use the PID to show the session cache preserves data across web site failovers.<\/p>\n<h2>Features<\/h2>\n<ul>\n<li><strong>Fast<\/strong>! Cache read access is typically in the 1 millisecond range, writes about 1.2 millisecond for the premium cache. The basic and standard caches are not quite that fast, but do provide a major speedup over typical database access (which is often in the hundreds to thousands of millisecond range.) <\/li>\n<li><strong>Ultra-fast<\/strong> <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/WindowsAzure\/dn386096.aspx\">Local Cache<\/a> &#8211; about 100 times faster than cache access.\u00a0 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/WindowsAzure\/dn386096.aspx\">Local Cache<\/a> is great in applications like the movie app or weather apps that can use recent data. I\u2019ll show how easy this is to configure. <\/li>\n<li><strong>Easy to set up, configure and use in your app<\/strong>. You can create a cache from 128MB to 150 GB. I\u2019ll show how to do that later in this tutorial. <\/li>\n<li><strong>Works with every cloud product.<\/strong> Use the same cache from worker roles, web roles, web sites, VMs (even Linux VMs). <\/li>\n<li><strong>Huge<\/strong>. You can create caches from 128MB all the way to 150 GB. <\/li>\n<li>**Reliable, high availability. **I\u2019ll show how to hook up session cache, restart the web site and show session data is preserved. No more need to set up session affinity. <\/li>\n<li><strong>Low cost<\/strong>. A cache can provide a cost effective approach to speeding up your app. See the <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=30000\">Capacity Planning Guide spreadsheet<\/a> for Windows Azure Caching. <\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn448830.aspx\">Multiple named caches<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn386134.aspx\">high availability<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn386096.aspx\">local cache<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn386095.aspx\">notifications<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn386102.aspx\">regions<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn386102.aspx\">tags<\/a>, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/jj160107\">Increment<\/a> <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/jj159979\">Decrement<\/a> APIs, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/jj160145\">Append<\/a> <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/jj160114\">Prepend<\/a> APIs, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/jj160035\">Cache level BulkGet<\/a> operations, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/dn386128.aspx\">expiration and eviction<\/a>,\u00a0 and more. See <a href=\"http:\/\/blogs.msdn.com\/b\/windowsazure\/archive\/2012\/06\/25\/cloud-services-series-what-s-new-in-windows-azure-caching.aspx\">What&#8217;s new in Windows Azure Caching<\/a> for details. <\/li>\n<\/ul>\n<h2>Set up the app to use Azure caching<\/h2>\n<h3>Create a cache on the Windows Azure portal<\/h3>\n<ol>\n<li>\n<p>Log into the Windows Azure portal, and click the <strong>New<\/strong> button on the bottom left of the screen.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/6505.New.png\"><img decoding=\"async\" title=\"New\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"New\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/4863.New_thumb.png\" width=\"690\" height=\"672\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>Click <strong>Data Services<\/strong>, <strong>Cache<\/strong>, and then <strong>Quick Create<\/strong>.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/6076.QuickCreate.png\"><img decoding=\"async\" title=\"QuickCreate\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"QuickCreate\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/2627.QuickCreate_thumb.png\" width=\"726\" height=\"391\" \/><\/a>\u00a0<\/p>\n<\/li>\n<li>\n<p>Enter an Endpoint and a region near you (where your web site will run). In this sample we&#8217;ll use the basic 128 MB cache.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/7838.endPt.png\"><img decoding=\"async\" title=\"endPt\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"endPt\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/7041.endPt_thumb.png\" width=\"534\" height=\"632\" \/><\/a><\/p>\n<p>You can use the <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=30000\">Capacity Planning Guide spreadsheet for Windows Azure Caching <\/a>to help you determine the appropriate cache size, performance and features for your application.<\/p>\n<\/li>\n<\/ol>\n<h2>Configure the cache<\/h2>\n<p>Select the <strong>Configure<\/strong> tab for Cache in the Management Portal.\u00a0 Each cache has a <strong>default<\/strong> named cache, and the Standard and Premium cache offerings support up to nine additional named caches, for a total of ten. Each named cache has its own set of options which allow you to configure your cache in a highly flexible manner.<\/p>\n<p>In the image below, the default cache is configured for a one minute sliding policy.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/0458.ConfigCache.png\"><img decoding=\"async\" title=\"ConfigCache\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"ConfigCache\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/0385.ConfigCache_thumb.png\" width=\"747\" height=\"276\" \/><\/a><\/p>\n<p>Each time a cache item is accessed, the expiration clock is reset. Setting it to one minute makes it easy to test the app.\u00a0 In the <a href=\"http:\/\/archive.msdn.microsoft.com\/Project\/Download\/FileDownload.aspx?ProjectName=aspnetmvcsamples&amp;DownloadId=16476\">download example<\/a>, each page shows the cache <em>Hit *or Miss status<\/em>, *so setting it to one minute makes it easy to show expirations. Cache aside algorithms generally use a sliding cache. You should configure the expiration for the longest period possible that doesn\u2019t overflow the cache and force excessive evictions. When the cache memory is full, new additions to the cache force evictions of the oldest items in the cache (LRU), and you pay a minor penalty waiting to evict the oldest cache items to make room for the new items. You can stress test your app and use the <strong>Monitor<\/strong> tab to see what percent of the cache capacity the app is using. (Monitoring is shown later in the tutorial.)<\/p>\n<h3>Configure the MvcMovie app for caching<\/h3>\n<ol>\n<li>\n<p>Use NuGet to install the Windows Azure Cache package. Select <strong>Online<\/strong> in the left tab and enter **caching **in the search box on the top right. Click the **Install **button for the Windows Azure Cache package.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/0602.NuGet.png\"><img decoding=\"async\" title=\"NuGet\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"NuGet\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1682.NuGet_thumb.png\" width=\"726\" height=\"575\" \/><\/a>\u00a0<\/p>\n<\/li>\n<li>\n<p>In the <strong>Select Projects **dialog click **OK<\/strong> and then click <strong>Install<\/strong>.<\/p>\n<\/li>\n<li>Open the root <em>web.config<\/em> file and find the <code>&lt;dataCacheClients&gt;<\/code> element at the bottom of the file. <\/li>\n<li>In the Windows Azure portal, copy the name of the <strong>Endpoint URL<\/strong> (<em>fixitcache.cache.windows.net<\/em> in the images above) and paste it into the <code>identifier<\/code>attribute of the <code>&lt;autoDiscover&gt;<\/code> element. <\/li>\n<li>\n<p>Remove the comment characters from the  element and paste in the authentication key. You can get the key from the portal by clicking on <strong>Manage Keys<\/strong> when you&#8217;ve selected your cache. The code below shows the markup you must change:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/6837.webCon.png\"><img decoding=\"async\" title=\"webCon\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"webCon\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/3323.webCon_thumb.png\" width=\"654\" height=\"176\" \/><\/a><\/p>\n<\/li>\n<\/ol>\n<h2>On Demand\/ Cache Aside code<\/h2>\n<p>There are many cache approaches you can take depending on your application needs. For this sample I\u2019ll use the on demand\/cache aside approach. Each query for a movie or list of movies first searches the cache. If the data is found in the cache, it\u2019s immediately returned without banging on the database. If the requested data is not in the cache, it uses the normal data access code, but then caches the data for the next time that data is requested. The following shows the <strong>getMovie<\/strong> method which is called by the <strong>Details<\/strong>, <strong>Edit<\/strong> and <strong>Delete<\/strong> action methods.<\/p>\n<pre class=\"code\"><span style=\"background: white;color: blue\">public class <\/span><span style=\"background: white;color: #2b91af\">MoviesController <\/span><span style=\"background: white;color: black\">: <\/span><span style=\"background: white;color: #2b91af\">Controller\n<\/span><span style=\"background: white;color: black\">{\n   <\/span><span style=\"background: white;color: blue\">private <\/span><span style=\"background: white;color: #2b91af\">MovieDBContext <\/span><span style=\"background: white;color: black\">db = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">MovieDBContext<\/span><span style=\"background: white;color: black\">();\n   <\/span><span style=\"background: white;color: blue\">private <\/span><span style=\"background: yellow;color: #2b91af\">DataCache <\/span><span style=\"background: white;color: black\">cache = <\/span><span style=\"background: white;color: blue\">new <\/span><span style=\"background: white;color: #2b91af\">DataCache<\/span><span style=\"background: white;color: black\">();\n\n   <\/span><span style=\"background: white;color: #2b91af\">Movie <\/span><span style=\"background: white;color: black\">getMovie(<\/span><span style=\"background: white;color: blue\">int <\/span><span style=\"background: white;color: black\">id)\n   {\n      <\/span><span style=\"background: white;color: #2b91af\">Stopwatch <\/span><span style=\"background: white;color: black\">sw = <\/span><span style=\"background: white;color: #2b91af\">Stopwatch<\/span><span style=\"background: white;color: black\">.StartNew();\n\n      <\/span><span style=\"background: white;color: #2b91af\">Movie <\/span><span style=\"background: white;color: black\">m = (<\/span><span style=\"background: white;color: #2b91af\">Movie<\/span><span style=\"background: yellow;color: black\">)cache.Get(id.ToString());\n\n      <\/span><span style=\"background: white;color: blue\">if <\/span><span style=\"background: white;color: black\">(m == <\/span><span style=\"background: white;color: blue\">null<\/span><span style=\"background: white;color: black\">)\n      {\n         <\/span><span style=\"background: white;color: #2b91af\">Movie <\/span><span style=\"background: white;color: black\">movie = db.Movies.Find(id);\n         &lt;mark&gt; cache.Put(id.ToString(), movie);&lt;\/mark&gt;\n         StopWatchMiss(sw);\n         <\/span><span style=\"background: white;color: blue\">return <\/span><span style=\"background: white;color: black\">movie;\n      }\n      StopWatchHit(sw);\n\n      <\/span><span style=\"background: white;color: blue\">return <\/span><span style=\"background: white;color: black\">m;\n   }<\/span><\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/p>\n<p>When a movie is edited or deleted, it is evicted from the cache:<\/p>\n<pre class=\"code\"><span style=\"background: white;color: blue\">void <\/span><span style=\"background: white;color: black\">ClearAllMoviesCache(<\/span><span style=\"background: white;color: blue\">int <\/span><span style=\"background: white;color: black\">id = 0)\n{\n   <\/span><span style=\"background: white;color: blue\">if <\/span><span style=\"background: white;color: black\">(id &gt; 0)\n   {\n      cache.Remove(id.ToString());\n   }\n   cache.Remove(<\/span><span style=\"background: white;color: #a31515\">\"All Movies\"<\/span><span style=\"background: white;color: black\">);\n}<\/span><\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a>Note the code above also clear out the cache entry \u201cAll Movies\u201d, which contains a list of all the movies and is used in the <strong>Index<\/strong> method.<\/p>\n<p>\u00a0<\/p>\n<h2>Download and run the sample<\/h2>\n<p><a href=\"http:\/\/archive.msdn.microsoft.com\/Project\/Download\/FileDownload.aspx?ProjectName=aspnetmvcsamples&amp;DownloadId=16476\">Download<\/a> and configure the sample using the instructions above. You must create a web site with a database. For instructions on deploying a web site with a database, see my tutorial <a href=\"http:\/\/www.windowsazure.com\/en-us\/develop\/net\/tutorials\/web-site-with-sql-database\/\">Deploy a Secure ASP.NET MVC 5 app with Membership, OAuth, and SQL Database to a Windows Azure Web Site<\/a>.<\/p>\n<p>The app is configured to display timing and cache status on each page.<\/p>\n<div class=\"note\">\n  <strong>Note: <\/strong>Latency between your desktop and the cloud service cache will be high because your desktop must access the cache service in the cloud. When your web app is located in the same region as the cache service, latency will be very low, in the one millisecond range.\n<\/div>\n<h2>Configure Local Cache<\/h2>\n<p>In the root <em>Web.config<\/em>\u00a0 file, find the  element and remove the comment characters. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/WindowsAzure\/dn386096.aspx\">Local Cache<\/a>, an ultra-fast second level cache stores cached items locally (in the memory of your web site or cloud service instance so there are no network hops or deserialization costs). Local Cache is hundreds of times faster than the cache service, reducing latency to the .01 millisecond range. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/WindowsAzure\/dn386096.aspx\">Local Cache<\/a> is not synchronized with the cache, so it shouldn&#8217;t be used where you need exact data values. However you can register for notifications to update the local cache at regular intervals from the cache cluster. Deploy the updated application and you will notice cache hits are now in the .01 millisecond range.<\/p>\n<h2>Monitor the Cache<\/h2>\n<p>From the portal, click on a cache endpoint, then click <strong>Monitor<\/strong>. The image below shows the cache usage data over the last hour. Note the read\/write requests per second are in units of a 1,000.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/5807.monitor.png\"><img decoding=\"async\" title=\"monitor\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"monitor\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1121.monitor_thumb.png\" width=\"648\" height=\"614\" \/><\/a><\/p>\n<p>Click the <strong>Add Metrics<\/strong> button on the bottom of the portal page to add additional metrics.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/7271.image_thumb_1FB46D89_2.png\"><img decoding=\"async\" title=\"image_thumb_1FB46D89\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"image_thumb_1FB46D89\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1732.image_thumb_1FB46D89_thumb.png\" width=\"572\" height=\"511\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<h2>ASP.NET session state using a cache provider<\/h2>\n<p>While it\u2019s considered a best practice is to avoid using session state, some applications can actually have a performance\/complexity benefit from using some session data, and other apps require session state.\u00a0 The default in memory provider for session state does not allow scale out (running multiple instances of the web site). The ASP.NET SQL Server session state provider will allow multiple web sites to use session state, but it incurs a high latency cost compared to an in memory provider. The ASP.NET session state cache provider is a low latency alternative that is very easy to configure and set up. If your app uses only a limited amount of session state, you can use most of the cache for caching data and a small amount for session state.<\/p>\n<h3>Configuring the ASP.NET <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/gg185668.aspx\">session state cache provider<\/a><\/h3>\n<p>You must install the Windows Azure Cache package using NuGet (see the instructions earlier in this tutorial). In the *Web.config *file, remove the comments from the <code>&lt;sessionState&gt;<\/code> and <code>&lt;caching&gt;<\/code>elements inside the\u00a0 <code>&lt;system.web&gt;<\/code> element. The following code shows the uncommented markup inside the <code>&lt;system.web&gt;<\/code> element:<\/p>\n<pre class=\"code\"><span style=\"background: white;color: blue\">  &lt;<\/span><span style=\"background: white;color: #a31515\">system.web<\/span><span style=\"background: white;color: blue\">&gt;\n    &lt;<\/span><span style=\"background: white;color: #a31515\">customErrors <\/span><span style=\"background: white;color: red\">mode<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">Off<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">\/&gt;\n    &lt;<\/span><span style=\"background: white;color: #a31515\">compilation <\/span><span style=\"background: white;color: red\">debug<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">true<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">targetFramework<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">4.5<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: blue\">\/&gt;\n    &lt;<\/span><span style=\"background: white;color: #a31515\">httpRuntime <\/span><span style=\"background: white;color: red\">targetFramework<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">4.5<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: blue\">\/&gt;\n  &lt;!-- <\/span><span style=\"background: white;color: green\">Windows Azure Cache session state provider <\/span><span style=\"background: white;color: blue\">--&gt;\n     &lt;<\/span><span style=\"background: white;color: #a31515\">sessionState <\/span><span style=\"background: white;color: red\">mode<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">Custom<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">customProvider<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">AFCacheSessionStateProvider<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">&gt;\n      &lt;<\/span><span style=\"background: white;color: #a31515\">providers<\/span><span style=\"background: white;color: blue\">&gt;\n        &lt;<\/span><span style=\"background: white;color: #a31515\">add <\/span><span style=\"background: white;color: red\">name<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">AFCacheSessionStateProvider<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">type<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">cacheName<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">default<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">dataCacheClientName<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">default<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">applicationName<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">AFCacheSessionState<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">\/&gt;\n      &lt;\/<\/span><span style=\"background: white;color: #a31515\">providers<\/span><span style=\"background: white;color: blue\">&gt;\n    &lt;\/<\/span><span style=\"background: white;color: #a31515\">sessionState<\/span><span style=\"background: white;color: blue\">&gt;\n     &lt;!-- <\/span><span style=\"background: white;color: green\">Windows Azure Cache output cache provider <\/span><span style=\"background: white;color: blue\">--&gt;&lt;!--<\/span><span style=\"background: white;color: green\">Uncomment this section to use Windows Azure Cache for output cache<\/span><span style=\"background: white;color: blue\">--&gt;\n     &lt;<\/span><span style=\"background: white;color: #a31515\">caching<\/span><span style=\"background: white;color: blue\">&gt;\n      &lt;<\/span><span style=\"background: white;color: #a31515\">outputCache <\/span><span style=\"background: white;color: red\">defaultProvider<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">AFCacheOutputCacheProvider<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">&gt;\n        &lt;<\/span><span style=\"background: white;color: #a31515\">providers<\/span><span style=\"background: white;color: blue\">&gt;\n          &lt;<\/span><span style=\"background: white;color: #a31515\">add <\/span><span style=\"background: white;color: red\">name<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">AFCacheOutputCacheProvider<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">type<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">cacheName<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">default<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">dataCacheClientName<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">default<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: red\">applicationName<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">\"<\/span><span style=\"background: white;color: blue\">AFCacheOutputCache<\/span><span style=\"background: white;color: black\">\" <\/span><span style=\"background: white;color: blue\">\/&gt;\n        &lt;\/<\/span><span style=\"background: white;color: #a31515\">providers<\/span><span style=\"background: white;color: blue\">&gt;\n      &lt;\/<\/span><span style=\"background: white;color: #a31515\">outputCache<\/span><span style=\"background: white;color: blue\">&gt;\n    &lt;\/<\/span><span style=\"background: white;color: #a31515\">caching<\/span><span style=\"background: white;color: blue\">&gt;\n  &lt;\/<\/span><span style=\"background: white;color: #a31515\">system.web<\/span><span style=\"background: white;color: blue\">&gt;<\/span><\/pre>\n<p>The sample download provides an easy mechanism to test session state is preserved on web site fail over. Run the download sample an click the <strong>Write Session<\/strong> button. You can optionally add route data to the session in the URL, such as <em>Movies\/WriteSession\/MyTestSessionData<\/em>.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/4201.ws.png\"><img decoding=\"async\" title=\"ws\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"ws\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8360.ws_thumb.png\" width=\"555\" height=\"331\" \/><\/a><\/p>\n<p>Click on the <strong>Read Session<\/strong> tab to read the session data. Now stop and restart the web site. (You can do this from the portal or from Visual Studio). Refresh the page or click the <strong>Read Session<\/strong> tab and you\u2019ll see the PID has changed.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1325.rs.png\"><img decoding=\"async\" title=\"rs\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"rs\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/7167.rs_thumb.png\" width=\"550\" height=\"341\" \/><\/a><\/p>\n<p>See <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/gg185668.aspx\">Session State Provider<\/a> for more details. Erez Benari has a great blog on <a href=\"http:\/\/blogs.msdn.com\/b\/windowsazure\/archive\/2013\/11\/18\/disabling-arr-s-instance-affinity-in-windows-azure-web-sites.aspx\">Disabling ARR\u2019s Instance Affinity in Windows Azure Web Sites<\/a> to allow you to use session state with multiple web sites, but with that approach, when a web site or host is updated, the load balancer sends subsequent requests to a new web site and all session data will be lost. If you app doesn\u2019t use much session state and you\u2019re using the already using the cache, you can get reliable and fast session state for practically free with the cache <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windowsazure\/gg185668.aspx\">Session State Provider<\/a>.<\/p>\n<h2>Caching Tips<\/h2>\n<ul>\n<li>For large objects, use <a href=\"http:\/\/code.google.com\/p\/protobuf-net\/\">protobuf-net<\/a>, <a href=\"https:\/\/github.com\/antmicro\/Migrant\">Migrant<\/a>, <a href=\"http:\/\/msgpack.org\/\">MessagePack<\/a> or another fast and flexible serialization framework. <\/li>\n<li>Objects larger than 8MB should first be compressed or split up. To enable compression, add the <em>isCompressionEnabled<\/em> attribute to the <em><\/em> element: <\/li>\n<\/ul>\n<p><span style=\"background: white;color: blue\">&lt;<\/span><span style=\"background: white;color: #a31515\">dataCacheClients<\/span><span style=\"background: white;color: blue\">&gt; <br \/>\u00a0\u00a0\u00a0 &lt;<\/span><span style=\"background: white;color: #a31515\">dataCacheClient <\/span><span style=\"background: white;color: red\">name<\/span><span style=\"background: white;color: blue\">=<\/span><span style=\"background: white;color: black\">&#8220;<\/span><span style=\"background: white;color: blue\">default<\/span><span style=\"background: white;color: black\">&#8221; <\/span><span style=\"background: yellow;color: red\">isCompressionEnabled<\/span><span>=<\/span><span style=\"background: white;color: black\">&#8220;<\/span><span style=\"background: white;color: blue\">true<\/span><span style=\"background: white;color: black\">&#8220;<\/span><span style=\"background: white;color: blue\">&gt;<\/span><span style=\"background: white;color: black\"> <br \/>\u00a0\u00a0\u00a0\u00a0 <\/span><span style=\"background: white;color: blue\"> <br \/>&lt;\/<\/span><span style=\"background: white;color: #a31515\">configuration<\/span><span style=\"background: white;color: blue\">&gt;<\/span><\/p>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a> \n*   The default the cache algorithm is well distributed,\u00a0 you don\u2019t need to manage it in your code, or worry about regions\/named caches. \n*   A region puts all the objects into a single instance which is useful for scenarios that need to keep related objects together (for example, you could have a region per user and keep multiple user objects user in a *user *region). Using regions allows you to bulk-query very efficiently. If you aren\u2019t using bulk operations or getting tagged objects from the cache, you don\u2019t need to create regions. \n*   A single named cache is always distributed across all the nodes in the cluster, so you don\u2019t need to create multiple named caches to distribute your data.<\/p>\n<p>Follow me ( <a href=\"https:\/\/twitter.com\/RickAndMSFT\">@RickAndMSFT<\/a> )\u00a0 on twitter where I have a no spam guarantee of quality tweets.<\/p>\n<h2>Resources:<\/h2>\n<p><a href=\"http:\/\/wacel.codeplex.com\/\">Windows Azure Cache Extension Library<\/a> WACEL implements high-level data structures that can be shared among your services and application. You can use WACEL data structures just as if you were using local data structures such as arrays, tables, circular buffers and OLAP cubes, and these data structures are backed by Windows Azure Cache, Windows Azure Table Storage, or both.<\/p>\n<p><a href=\"http:\/\/wag.codeplex.com\/releases\/view\/113604\">Microsoft Patterns and Practices &#8211; Windows Azure Guidance<\/a>. See Caching guidance and Cache-Aside pattern.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Warning. This is obsolete, see my new blog MVC movie app with Azure Redis Cache in 15 minutes Warning: Most of the information in this blog is for the Preview edition and should not be used or targeted for production code. Use the In-Role Cache for production code. In this blog post I\u2019ll take my [&hellip;]<\/p>\n","protected":false},"author":415,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-22021","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet"],"acf":[],"blog_post_summary":"<p>Warning. This is obsolete, see my new blog MVC movie app with Azure Redis Cache in 15 minutes Warning: Most of the information in this blog is for the Preview edition and should not be used or targeted for production code. Use the In-Role Cache for production code. In this blog post I\u2019ll take my [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/22021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/415"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=22021"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/22021\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=22021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=22021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=22021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}