Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / December 2003

Tip: Looking for answers? Try searching our database.

Cache vs Class Variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jay Lindsay - 01 Dec 2003 17:10 GMT
Question 1:  What is the difference between using Cache/Session/Application vs using a Class (shared) variable?

Question 2:  I have a class that keeps track of user preferences.  It inherits directly from Object.  I am using a class (shared) variable to store a list of preferences that applies to all users.  I have a method that returns these preferences.  In that method I check to make  sure the collection of preferences has been set, if not I access the database to retrieve the preferences.  This seems to work fine.  I cannot use Cache, etc. because they can only be used from Page.

Is there any problem with using class (shared) variables as I have explained.

Signature

Jay

Alvin Bruney - 01 Dec 2003 17:51 GMT
Question 1:  What is the difference between using Cache/Session/Application vs using a Class (shared) variable?

Bunch of stuff, it will be faster to use a class because of serialization overhead associated with session. It's not going to be noticeable for what you are doing though. Using a class gives you an extensible way of handling all that information

The downside is you need to implement synchronization in your class because it isn't automatically done for you like in cache and session.

I cannot use Cache, etc. because they can only be used from Page
That's not correct.

You have a good approach, I would stick with it if I were you. Just pay attention to synchronization issues where 2 or more clients can access the class at the same time.

Signature

Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits

 Question 1:  What is the difference between using Cache/Session/Application vs using a Class (shared) variable?

 Question 2:  I have a class that keeps track of user preferences.  It inherits directly from Object.  I am using a class (shared) variable to store a list of preferences that applies to all users.  I have a method that returns these preferences.  In that method I check to make  sure the collection of preferences has been set, if not I access the database to retrieve the preferences.  This seems to work fine.  I cannot use Cache, etc. because they can only be used from Page.

 Is there any problem with using class (shared) variables as I have explained.

 --
 Jay
Jerry III - 02 Dec 2003 03:18 GMT
A cache does not serialize objects. It simply stores references to them. The
only overhead is the name lookup, which unless you store thousands of
objects is a very quick operation.

Jerry

Question 1:  What is the difference between using Cache/Session/Application
vs using a Class (shared) variable?

Bunch of stuff, it will be faster to use a class because of serialization
overhead associated with session. It's not going to be noticeable for what
you are doing though. Using a class gives you an extensible way of handling
all that information

The downside is you need to implement synchronization in your class because
it isn't automatically done for you like in cache and session.

I cannot use Cache, etc. because they can only be used from Page
That's not correct.

You have a good approach, I would stick with it if I were you. Just pay
attention to synchronization issues where 2 or more clients can access the
class at the same time.

--
Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits
 "Jay Lindsay" <EJLindsay@email.uophx.edu> wrote in message
news:OAhtG4CuDHA.2464@TK2MSFTNGP12.phx.gbl...
 Question 1:  What is the difference between using
Cache/Session/Application vs using a Class (shared) variable?

 Question 2:  I have a class that keeps track of user preferences.  It
inherits directly from Object.  I am using a class (shared) variable to
store a list of preferences that applies to all users.  I have a method that
returns these preferences.  In that method I check to make  sure the
collection of preferences has been set, if not I access the database to
retrieve the preferences.  This seems to work fine.  I cannot use Cache,
etc. because they can only be used from Page.

 Is there any problem with using class (shared) variables as I have
explained.

 --
 Jay
Alvin Bruney - 02 Dec 2003 04:19 GMT
You're right. But I never said that. What I said was

Bunch of stuff, it will be faster to use a class because of serialization
overhead associated with session

The
> only overhead is the name lookup, which unless you store thousands of
> objects is a very quick operation.

The only over head is not just a name look up since the hashtable access has
to be cast back into an object of an appropriate type since the hashtable
stores its items as dictionaryitem. This access is not as fast as, say -
reading an accessor which returns someobject.ToString() since a tostring()
operation boils down to an internal pointer on the object - in essense, a
direct read.

It's quick but it's not quick as a class property read which is why i
recommended it in the first place.

Signature

Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits

> A cache does not serialize objects. It simply stores references to them. The
> only overhead is the name lookup, which unless you store thousands of
[quoted text clipped - 43 lines]
>   --
>   Jay
Jay Lindsay - 02 Dec 2003 13:09 GMT
Thanks for your responses.

How do I use Cache (or Session and Application) from a class that inherits from object?

Signature

Jay

Jimmy [Used-Disks] - 04 Dec 2003 14:31 GMT
> How do I use Cache (or Session and
> Application) from a class that
> inherits from object?

Here's a quick example:

public class SiteApplication
{
 public static MyClass MyInstance{
   get{
     object Result =
HttpContext.Current.Application["SiteApplication.MyInstance"];
     if(Result == null){
       Result = new MyClass();
       HttpContext.Current.Application["SiteApplication.MyInstance"] =
Result;
   }
   return (MyClass)Result;
 }
}

Signature

-Jimmy

Jay Lindsay - 02 Dec 2003 13:16 GMT
Now that I have implemented my need for "caching" with a class variable I see that there is a problem.  To review:  I am using a class (shared) variable to store a list of preferences that applies to all users.  I have a method that returns these preferences.  In that method I check to make  sure the collection of preferences has been set, if not I access the database to retrieve the preferences.   When I insert a new item in the database that item will not be read by my class because the collection of preferences has already been set.  By using Cache I can invalidate my cache when I insert a new item in the database so that the cache will be recreated.

Any thoughts?

This leads me back to my other question of how can I use Cache in class that inherits directly from Object.

----
Jay
Alvin Bruney - 02 Dec 2003 17:32 GMT
Put a static dirty flag variable in there, set an accessor to read this dirty flag - IsValid
When you write to the database, set IsValid to false. Every access to the class should check this property to know whether the cache is valid or not. If it is valid, read it, otherwise, read from the database and update the flag to true.

Lock it as well before you touch it. Roughly, the code should look like this. This will be thread safe *mostly* because you have all that stuff in the global class you have to protect it from concurrent reads/writes.

private static bool isValid = false;

public bool IsValid
{
   get
   {
       lock(this)
       {
            isValid ? return isValid : return !isValid;  
       }
   }
   set
   {
       lock(this)
       {
            isValid = value;  
       }
   }
}

Signature

Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits

 Now that I have implemented my need for "caching" with a class variable I see that there is a problem.  To review:  I am using a class (shared) variable to store a list of preferences that applies to all users.  I have a method that returns these preferences.  In that method I check to make  sure the collection of preferences has been set, if not I access the database to retrieve the preferences.   When I insert a new item in the database that item will not be read by my class because the collection of preferences has already been set.  By using Cache I can invalidate my cache when I insert a new item in the database so that the cache will be recreated.

 Any thoughts?

 This leads me back to my other question of how can I use Cache in class that inherits directly from Object.

 ----
 Jay
Jerry III - 03 Dec 2003 09:48 GMT
Why so difficult? You can simply update the variable holding the value on a
database write (under a lock of course), where you have all the data ready.
Instead of just setting a dirty flag and then re-requesting the data you
already had from a database.

Jerry

Put a static dirty flag variable in there, set an accessor to read this
dirty flag - IsValid
When you write to the database, set IsValid to false. Every access to the
class should check this property to know whether the cache is valid or not.
If it is valid, read it, otherwise, read from the database and update the
flag to true.

Lock it as well before you touch it. Roughly, the code should look like
this. This will be thread safe *mostly* because you have all that stuff in
the global class you have to protect it from concurrent reads/writes.

private static bool isValid = false;

public bool IsValid
{
   get
   {
       lock(this)
       {
            isValid ? return isValid : return !isValid;
       }
   }
   set
   {
       lock(this)
       {
            isValid = value;
       }
   }
}

--
Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits
 "Jay Lindsay" <JayLindsay@comcast.net> wrote in message
news:uctI4ZNuDHA.2224@TK2MSFTNGP09.phx.gbl...
 Now that I have implemented my need for "caching" with a class variable I
see that there is a problem.  To review:  I am using a class (shared)
variable to store a list of preferences that applies to all users.  I have a
method that returns these preferences.  In that method I check to make  sure
the collection of preferences has been set, if not I access the database to
retrieve the preferences.   When I insert a new item in the database that
item will not be read by my class because the collection of preferences has
already been set.  By using Cache I can invalidate my cache when I insert a
new item in the database so that the cache will be recreated.

 Any thoughts?

 This leads me back to my other question of how can I use Cache in class
that inherits directly from Object.

 ----
 Jay
Jay Lindsay - 03 Dec 2003 22:10 GMT
Please explain in a little more detail.

Signature

Jay

Jerry III - 04 Dec 2003 12:03 GMT
Jay,

what's to explain?

To read the object (either cached or not):

   Object Value = Cache["MyObject"];
   if( Value == null )
   {
       // Load Value Here
       Cache["MyObject"] = Value;
   }
   // Value now has your object

And to write it (when you want to update it):

   Object Value = null;    // Store the new value here

   // Now save it to the database

   // And finally update the cached object
   Cache["MyObject"] = Value;

It does not make sense to save the value to the cache when you have it and
wait until the first read access to retrieve it.

Jerry

Please explain in a little more detail.

--
Jay
Anders Borum - 04 Dec 2003 21:47 GMT
Jerry, you forgot the locking part of your example! Sorry, couldn't resist
it ;-)

Signature

venlig hilsen / with regards
anders borum
--

Jerry III - 05 Dec 2003 13:32 GMT
Well, you don't really have to lock anything. The Cache class has its own
locking and it's not necessary to put yet another lock around it. I
should've removed the lock variable.

Jerry

> Jerry, you forgot the locking part of your example! Sorry, couldn't resist
> it ;-)
[quoted text clipped - 3 lines]
> anders borum
> --

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.