首页 > 代码库 > 应用程序缓存
应用程序缓存
页面:
<asp:Label ID="lblId" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblClick" runat="server" Text="Label"></asp:Label>
后台:
if (!IsPostBack) {
int id = Convert.ToInt32(Request["id"]);
Books books = null;
if (Cache["Books_" + id] != null) {
//从内存中读取
books = Cache["Books_" + id] as Books;
}
else
{
//请求数据库
books = new BooksManager().getIdBooksInfo(id);
if (books!=null&&books.Clicks>200)
{
//添加应用程序缓存
Cache["Books_" + id] = books;
}
}
if (books!=null)
{
this.lblId.Text = Convert.ToString(books.Id);
this.lblTitle.Text = books.Title;
this.lblClick.Text = Convert.ToString(books.Clicks);
}
应用程序缓存