title: Caching data in ASPNET MVC SinglePageApplication
body: <p>I´m looking for a solution or best practice concerning my problem. Since it is more an architecture-challenge I have got no sourcecode...</p>
  
  <p>My problem is how I can deal with the ASP.NET session concerning my requirements. Or does it not fulfill my requirements?</p>
  
  <p>Situation: I`m having a SinglePageApplication build on the top of ASP.NET MVC. The MVC-app provides the server-side interface to deal with the backend. The MVC-app calls services of a a different backend-server to request data. This data could be a lot and should be cached in the MVC-app.</p>
  
  <p>As far as I see the ASP.NET-session does not seem to be the best solution for SPAs since its concurrent behaviour... ?! It is not possible to have concurrent ajax-requests if there is any request which requries write-access to the session (and that happens in my case since data has to be cached).</p>
  
  <p>Example: Typical situation, at least in my application, is that when a user accesses a certain screen some ajax-requests are performed. E.g. the first request triggers loading data from the backend-server and takes some time (maybe about 30seconds...). It also requires write-access to the session since it should cache the data for this(!) user. Other requests now have to wait even though they only require read-access to the session and do not deal with the data to load. The behaviour that one request waits for another one should only happen in certain situations.</p>
  
  <p>I could now implement a different mechanism to cache data for each user maybe using <em>System.Runtime.Caching</em> but is this the recommended or best solution?</p>
  
  <p>What would be the best pattern to deal with that situation? </p>
  
  <p>Are there any best practices?</p>
  
  <p><strong>Edit2:</strong></p>
  
  <p>One big point is the behaviour of the session when performing concurrent ajax-calls. I have prepared a small example: An application performing ajax-calls - some are using the session as ReadOnly - other ones are requiring ReadWrite. The actions have a timeout the issue more visible...</p>
  
  <p>These are my controllers:</p>
  
  <pre><code>[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
  public class SessionReadOnlyController : Controller
  {
      public ActionResult Perform()
      {
          Session["x"] = 5;
  
          Thread.Sleep(1500);
          return Json(new { success = true }, JsonRequestBehavior.AllowGet);
      }
  }
  
  [SessionState(System.Web.SessionState.SessionStateBehavior.Required)]
  public class SessionReadWriteController : Controller
  {
      public ActionResult Perform()
      {
          Session["x"] = 5;
  
          Thread.Sleep(1500);
          return Json(new { success = true }, JsonRequestBehavior.AllowGet);
      }
  }
  </code></pre>
  
  <p>This is the javascript-code which performs the ajax-calls. All calls are performed immediately:</p>
  
  <pre><code>jQuery(function ()
  {
      $('body').append('&lt;p&gt;start....1&lt;/p&gt;');
      $.ajax({
          url: '/SessionReadWrite/Perform?id=1',
          success: function () { $('body').append('&lt;p&gt;success1&lt;/p&gt;'); }
      });
  
      $('body').append('&lt;p&gt;start....2&lt;/p&gt;');
      $.ajax({
          url: '/SessionReadWrite/Perform?id=2',
          success: function () { $('body').append('&lt;p&gt;success2&lt;/p&gt;'); }
      });
  
      $('body').append('&lt;p&gt;start....3&lt;/p&gt;');
      $.ajax({
          url: '/SessionReadOnly/Perform?id=3',
          success: function () { $('body').append('&lt;p&gt;success3&lt;/p&gt;'); }
      });
  
      $('body').append('&lt;p&gt;start....4&lt;/p&gt;');
      $.ajax({
          url: '/SessionReadOnly/Perform?id=4',
          success: function () { $('body').append('&lt;p&gt;success4&lt;/p&gt;'); }
      });
  
      $('body').append('&lt;p&gt;start....5&lt;/p&gt;');
      $.ajax({
          url: '/SessionReadOnly/Perform?id=5',
          success: function () { $('body').append('&lt;p&gt;success5&lt;/p&gt;'); }
      });
  });
  </code></pre>
  
  <p>The results in my case (taken from the developer-tool of the internet-explorer):</p>
  
  <ul>
  <li>Call 1 responds after 1,51s</li>
  <li>Call 2 responds after 3,03s</li>
  <li>Call 3 responds after 4,56s</li>
  <li>Call 4 responds after 4,56s</li>
  <li>Call 5 responds after 4,56s</li>
  </ul>
  
  <p>As I understand the behaviour of the situation the ReadWrite-Session-actions locking the session even for ReadOnly-Session-actions...?! (Or did I made something totally wrong?)</p>
  
  <p>From msdn ( <a href="http://msdn.microsoft.com/de-de/library/ms178581%28v=vs.100%29.aspx" rel="nofollow">http://msdn.microsoft.com/de-de/library/ms178581%28v=vs.100%29.aspx</a> ), last chapter: </p>
  
  <blockquote>
    <p>[...] However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. [...] However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.</p>
  </blockquote>
  
  <p>Concerning my situation: If I want to cache data (in-memory is okay because the application is used by just a few users) in a SinglePageApplication I need a different concept than the session... therefore my question, are there best practices?</p>
  
  <p><em>Edit: Improved description of situation...</em></p>
  
  <p><em>Edit2: Added an example...</em></p>