2012-03-29 26 views
2

我正在尋找一個(如果可能)詳細解釋爲什麼下面的事情正在發生:asp.net MVC 3怪異的WebCache行爲

我使用asp.net的靜態的WebCache類裏面存放我的對象

public ActionResult Index(int page = 0) 
    { 

     const int count = 5; 

     var recordsToSkip = page*count; 
     if (WebCache.Get("page-" + page + "-5") == null) 
     { 
      var records = 
       db.Submissions.OrderByDescending(x => x.SubmitedOn).Skip(recordsToSkip).Take(count).ToList(); 
      var index = new IndexViewModel() 
          { 
           ChallengeEntries = records, 
           CurrentPage = 0 
          }; 
      WebCache.Set("page-" + page + "-5", index); 
     } 

     return View(WebCache.Get("page-" + page + "-5")); 
    } 

,我的看法是:

<div class="pagination"> 
     @Model.CurrentPage // here just to let me watch the value of the current page 
     @if(Model.CurrentPage >=1) 
     { 


      @Html.ActionLink("Previous", "Index", new { page = Model.CurrentPage-- }) 
     } 

     @if(Model.ChallengeEntries.Count()>=5) 
     { 

      @Html.ActionLink("Next", "Index", new { page = Model.CurrentPage++ }) 
     } 

    </div> 

所以我不知道爲什麼是Model.CurrentPage++增加當前緩存對象的值並保存它,而不是僅僅返回值+1而不是修改緩存?

回答

3

當您從緩存訪問時,您正在訪問緩存中的對象的引用。

這就是爲什麼增加它的值時它正在更新並保存在緩存中。

+0

這就是我想的,但不知道。 – Aviatrix 2012-04-02 16:46:15