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); // 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With