This simple cache class needs thread synchronization ... if I remove the lock, will the _syncLock operator run into any problems? I think I can remove the locks because the links should be edited correctly correctly? ... BUt I think that whar happens if the client code iterates using the GetMyDataStructure method and is replaced?
Edit: I replaced GetMyDataStructure with the TryGetValue style method and removed all the locks ... should this be fine?
public bool TryGetValue(int id, out MyDataStructure myDataStructure)
{
return _cache.TryGetValue(id, out myDataStructure);
}
Thank!
public sealed class Cache
{
private readonly object _syncLock = new object();
private IDictionary<int, MyDataStructure> _cache;
public Cache()
{
Refresh();
}
public void Refresh()
{
lock (_syncLock)
{
_cache = DAL.GetMyDataStructure();
}
}
public IDictionary<int, MyDataStructure> **GetMyDataStructure**()
{
lock (_syncLock)
{
return _cache;
}
}
}
source
share