R
Ryan
The real reason for my previous post about intercepting
Application_EndRequest (from outside of Global.asax) is because of a need to
create a "non persistent page level cache". I can't seem to find an
intrinsic property that exposes a Hashtable that gets reset for every page
request. If someone knows of such a property, please let me know!
Page.Cache is alive through an application domain. ViewState, of course, is
persisted between postbacks. There does not appear to be a cache-type
property which is only alive for the life of a page request. Below is some
code that I am using...everything would work fine if I could just get my
EndRequest function to be called!
namespace MyApp
{
using System;
using System.Collections;
using System.Web;
public class NonPersistentPageCache
{
static private Hashtable _cache;
private Hashtable _hash;
static public NonPersistentPageCache Cache
{
get
{
if (_cache == null)
_cache = new System.Collections.Hashtable();
int thisPageHashCode = HttpContext.Current.GetHashCode();
if (_cache[thisPageHashCode] == null)
{
NonPersistentPageCache oNonPersistentPage =
new NonPersistentPageCache();
HttpContext.Current.ApplicationInstance.EndRequest +=
new EventHandler(Application_EndRequest);
_cache.Add(thisPageHashCode, oNonPersistentPage);
}
return (NonPersistentPageCache) _cache[thisPageHashCode];
}
}
public void Application_EndRequest(object sender, EventArgs e)
{
int thisPageHashCode = HttpContext.Current.GetHashCode();
if (_cache != null)
_cache.Remove(thisPageHashCode);
}
private NonPersistentPageCache()
{
_hash = new System.Collections.Hashtable();
}
public object this[string key]
{
get { return _hash[key]; }
set { _hash[key] = value; }
}
}
}
I then use this class as required:
NonPersistentPageCache.Cache["whatever"] = "whatever";
Thanks,
Ryan
Application_EndRequest (from outside of Global.asax) is because of a need to
create a "non persistent page level cache". I can't seem to find an
intrinsic property that exposes a Hashtable that gets reset for every page
request. If someone knows of such a property, please let me know!
Page.Cache is alive through an application domain. ViewState, of course, is
persisted between postbacks. There does not appear to be a cache-type
property which is only alive for the life of a page request. Below is some
code that I am using...everything would work fine if I could just get my
EndRequest function to be called!
namespace MyApp
{
using System;
using System.Collections;
using System.Web;
public class NonPersistentPageCache
{
static private Hashtable _cache;
private Hashtable _hash;
static public NonPersistentPageCache Cache
{
get
{
if (_cache == null)
_cache = new System.Collections.Hashtable();
int thisPageHashCode = HttpContext.Current.GetHashCode();
if (_cache[thisPageHashCode] == null)
{
NonPersistentPageCache oNonPersistentPage =
new NonPersistentPageCache();
HttpContext.Current.ApplicationInstance.EndRequest +=
new EventHandler(Application_EndRequest);
_cache.Add(thisPageHashCode, oNonPersistentPage);
}
return (NonPersistentPageCache) _cache[thisPageHashCode];
}
}
public void Application_EndRequest(object sender, EventArgs e)
{
int thisPageHashCode = HttpContext.Current.GetHashCode();
if (_cache != null)
_cache.Remove(thisPageHashCode);
}
private NonPersistentPageCache()
{
_hash = new System.Collections.Hashtable();
}
public object this[string key]
{
get { return _hash[key]; }
set { _hash[key] = value; }
}
}
}
I then use this class as required:
NonPersistentPageCache.Cache["whatever"] = "whatever";
Thanks,
Ryan