Using System.Web.Caching.Cache
I am trying to use the Cache, but get the error below. How can I properly use the 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开发者_Go百科); // 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' is a 'type' but is used like a 'variable'
System.Web.Caching.Cache
: this is the implementation of .NET caching.
System.Web.HttpContext.Current.Cache
: this is the instance of that implementation, that lives in the application domain.
I think you want to use the second one if you are not in the code behind of an aspx page. Use Cache if you are in the code behind of an aspx page.
You can also use Page.Cache.Insert
directly that has a reference to the System.Caching.Cache
through the page object. All this point to the same application cache which are global for all users.
Have somewhere that the class stores the HttpContext when it is initialized by new, or with an Init()
method
Then use the HttpContext.Current.Cache
Or: Make methods to read and write to the cache with a parameter currentcache
, and document that so with sample code where it is called with HttpContext.Current.Cache
精彩评论