[英]Using System.Web.Caching.Cache
我正在嘗試使用緩存,但得到以下錯誤。 如何正確使用緩存?
protected void Page_Load(object sender, EventArgs e) {
x = System.DateTime.Now.ToString();
if (Cache["ModifiedOn"] == null) { // first time so no key/value in Cache
Cache.Insert("ModifiedOn", x); // inserts the key/value pair "Modified On", x
}
else { // Key/value pair already exists in the cache
x = Cache["ModifiedOn"].ToString();
} }
'System.Web.Caching.Cache' 是一種'類型',但用作'變量'
System.Web.Caching.Cache
:這是 .NET 緩存的實現。
System.Web.HttpContext.Current.Cache
:這是該實現的實例,位於應用程序域中。
如果您不在 aspx 頁面后面的代碼中,我認為您想使用第二個。 如果您在 aspx 頁面的代碼中,請使用緩存。
您還可以通過頁面Page.Cache.Insert
引用System.Caching.Cache
。 所有這些都指向同一個應用程序緩存,它對所有用戶都是全局的。
當 class 由 new 或使用Init()
方法初始化時,在某處存儲 HttpContext
然后使用HttpContext.Current.Cache
或者:使用參數currentcache
讀取和寫入緩存的方法,並使用示例代碼記錄該方法,其中使用HttpContext.Current.Cache
調用它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.