473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

caching and locking

For my middle layer objects, I have a base class that other classes
subclass. This base class houses properties that all of my objects (with
some exceptions) have.

Here is the problem. One of the properties the client can configure by

1) setting it himself.
2) accessor can set it up by readign it from the config file.

I am caching the value read from the config file using a key set by the
client. The same client can create different objects with different
keys. My concern is the code that reads the value from the cache. Since
I am using the middle layer from the web and different asp.net worker
threads might want to set the property up:

// cahce is static variable here

public string Key
{
get{;}
set{;}
}
public string SomeValue
{
get
{
if (cache.Contains(Key)
return cache[Key];
else
{
lock
{
cache.Add(Key, readFromConfig(Key));
}
}
}
}

Is it ok that I use locking here? The condition might happen that
multiple threads will not find the value based on key and one will add,
and then another will want to add with the same key.

Is my approach ok here? I could also catch exception of duplicate key
and assume that someone added the value.

What do you guys think?
Nov 17 '05 #1
3 2771
laimis <si*****@iit.edu> wrote:

<snip>
Is it ok that I use locking here? The condition might happen that
multiple threads will not find the value based on key and one will add,
and then another will want to add with the same key.

Is my approach ok here? I could also catch exception of duplicate key
and assume that someone added the value.

What do you guys think?


You should lock around the checking as well - effectively make the
checking and potential adding atomic.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

You should lock around the checking as well - effectively make the
checking and potential adding atomic.


Actually here is how I am doing get part:

public string SomeValue
{
get
{
if (cache.Contains(Key)
return cache[Key];
else
{
lock
{
if (cache.Contains(Key)
return cache[Key];
else
cache.Add(Key, readFromConfig(Key));
}
}
}
}

This way, I don't lock first time when checking the cache. So that there
is no delay when the key exists. In the lock, I am checking again to
make sure that the key was not added previously ...

So I guess Jon you are saying this would be a fine approach? No concerns?
Nov 17 '05 #3
laimis <si*****@iit.edu> wrote:
You should lock around the checking as well - effectively make the
checking and potential adding atomic.


Actually here is how I am doing get part:

public string SomeValue
{
get
{
if (cache.Contains(Key)
return cache[Key];
else
{
lock
{
if (cache.Contains(Key)
return cache[Key];
else
cache.Add(Key, readFromConfig(Key));
}
}
}
}

This way, I don't lock first time when checking the cache. So that there
is no delay when the key exists. In the lock, I am checking again to
make sure that the key was not added previously ...

So I guess Jon you are saying this would be a fine approach? No concerns?


No, what I said was that you need a lock round the whole thing.

Locks are pretty cheap - don't risk thread safety by trying to avoid
them. There are *very* subtle concerns when it comes to using shared
data.

It's possible (according to the CLI spec) for the cache addition to
become visible to other threads before all the memory writes in
readFromConfig. This would mean it's possible for one thread to be in
the process of adding an object to the cache, and another object could
read that cache entry and see an incomplete object.

It almost certainly won't happen in the current implementation of the
CLI, but the performance gain you'll get from avoiding the lock is so
small that it's not worth the risk, IMO.

See http://www.pobox.com/~skeet/csharp/t...latility.shtml for
more details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matthew Bates | last post by:
Hi, I've been using PHP for a while now and I'm beginning to migrate to PHP5. So far, I'm impressed. I need to develop an admin application to enable users to update a website that is built...
1
by: siliconmike | last post by:
I've configured apache to fetch filename.htm if user requests filename.htm. But if filename.htm does not exist, apache will call index.php. Now, index.php will generate a page, buffer it using...
9
by: J. Baute | last post by:
I'm caching data in the Application object to speed up certain pages on a website The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data...
3
by: Gary W. Smith | last post by:
I had a couple questions about data caching. We have a site that gets a huge amount of traffic to a few specific pages that have a lot of data on them (300k hits/hour during peak, about 6-10 data...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.