我正在寻找(如果可能的话)详细解释为什么会发生以下事情:
我正在使用 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而不修改缓存?
最佳答案
当您从缓存中访问时,您正在访问缓存中对象的引用。
这就是为什么当增加它的值时,它会被更新并保存在缓存中。
关于c# - asp.net mvc 3 WebCache 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934165/