Joerg Jooss
1/19/2006 9:53:00 PM
Hello RickG,
> 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?
A member's protection level is meaningless in this context. And using public
member is a plain aweful practice :-S
> 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.
You didn't write in what kind of class you have created this public variable.
Is it static as well?
> The question is, am I going to have some problem doing it this way,
> and if not, what benefit does the cache object have?
If the hashtable was a non-static member of a Page class, it's lifetime would
be one HTTP request/response exchange. Thus, every page hit would become
a database hit. The cache on the other hand persists objects throughout the
ASP.NET's worker process lifetime (sort of a simplified view, but anyway).
Thus, there'll be only one database hit for as long as your hashtable isn't
removed from the cache.
Yes, you can get the same basic lifetime behaviour with a static class member,
but not cache's advanced functionality like automatic removal of "aged" objects
if you're running low on memory, depedencies on external resources like files
etc.
Cheers,
--
Joerg Jooss
news-reply@joergjooss.de