Non persistent page cache

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
 
P

Paul Glavich

Ryan,

Have you investigated the [ThreadStatic] attribute? This attribute can be
applied to variables, which Indicates that the value of a static field is
unique for each thread. Given that each request is processed on a different
thread (usually) it might be easier than the method you describe below. The
fact that the system uses a threadpool though may cause some undesired
effects so you want to test this.

Although I am not entirely clear why you dont use a variable that is scoped
for the entire page class (eg. at the top of the class defn, just have
'private object _myVar') and assign it in page load. When the page request
is finished processing and the response sent back to the client, the value
is lost anyway. Perhaps I am not fully understanding your needs though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top