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?