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
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
> --