Caching vs. public variables in vb.net 2003 web app

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anybody clarify this please.
Is there any problem in a web app of saving an object in a public variable
as opposed to saving it in the cache?

In my case, I have an object containing a hash table of text labels and
their keys which I save in a public variable.

Any access to the object causes it to check if it has been initialised, and
if not it loads the hash table from the database, at which point it logs a
message to indicate the load has occurred. On examination of the log, I only
see it load once when the application starts, which is waht I want it to do.

The question is, am I going to have some problem doing it this way, and if
not, what benefit does the cache object have?

Thanks
Rick Gwadera
Software designer, programmer etc.
 
Rick,
I'm going on a limb here and I won't claim to be an expert but I guess
one of the questions related to your question would be "how will your
public variable survive garbage collection?" If the variable is being
used repeatedly while the application is running, then it will tend to
survive, but I've also seen cached items get disposed of when I was not
expecting it. However, nearly everything can also be cached and there
are the persistence properties for the Caching class that can be set
for a cached item.

I once had a drop down list whose datasource was a DataSet that was
part of a user control that sat in every page in my application. And
although the dataset was cached there would be times when the page
would load and the drop down list would be empty. After putting some
code around the cached dataset code to check if it was not null, I was
able to fix the problem. I ended up doing something along these
lines...

DataSet dsDropDownList;
public DataSet DsDropDownList
{
get
{
if (dsDropDownList == null)
{
if (Cache["DsDropDownList"] == null)
{
dsDropDownList = GetDataSet();
}
else
{
dsDropDownList = (DataSet)Cache["DsDropDownList"];
}
}
if ((string)Context.Cache["UpdateDataSet"] == "true")
{
lock(this)
{
dsDropDownList = GetDataSet();
Cache["UpdateDataSet"] = "false";
}
}
return dsDropDownList;
}
}
I don't know if this will help but I think ultimately, time permitting,
you might want to try both ways of doing it to see which one suits you
best. HTH. Good luck!
Eric
 
Back
Top