Nikola Sivkov
Nikola Sivkov

Reputation: 2852

asp.net mvc 3 WebCache weird behaviour

I'm looking for a (if possible) detailed explanation to why the following thing is happening :

I'm using asp.net's WebCache static class to store my object inside

   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"));
    }

and my view is :

<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> 

So I'm wondering why is the Model.CurrentPage++ increasing the value of the current cached object and saving it instead of just returning the value +1 and not modifying the cache ?

Upvotes: 2

Views: 412

Answers (1)

Neelesh
Neelesh

Reputation: 496

When you are accessing from the cache, you are accessing to the reference of the object in the cache.

That's why when incrementing it's value it is being updated and saved in the cache.

Upvotes: 3

Related Questions