数据缓存
ASP.Net 公开 Cache API 以将数据存储在缓存中以便以后检索。
存储字符串
Cache["key"]="value";
检索字符串
var value="";
if (Cache["key"] != null)
value = Cache["key"].ToString();
你还可以使用“ 添加” 或“ 插入” 方法。
protected void Page_Load( object sender, EventArgs e)
{
if ( this.IsPostBack )
{
label1.Text + = "Page is posted back";
}
else
{
label1.Text + = "Page is created";
}
if ( Cache [ "item"] == null )
{
label1.Text + = "New item is created";
DateTime item = DateTime.Now;
label1.Text + = "Item is stored";
Cache.Insert ( "item", item, null );
DateTime.Now.AddSeconds ( 20 ), TimeSpan.Zero;
}
else
{
label1.Text + = "Item is accesses";
DateTime item = ( DateTime) Cache [ "item" ];
label1.Text + = "Time is: " + item.ToString();
label1.Text + = <br/>";
}
label1.Text + = "<br/>";
}