I have singleton that fetches from DB, hence it is expensive load.
It is lazy loaded.
I would like to create a method that refreshes that singleton and populates it once it is required.
the data is DB and very expensive, so I want to refresh it only once in case I have concurrent calls. (that is, if I get 500 calls to refresh, I want to restart the refresh only once)
public static PageData Instance
{
get
{
if (m_Instance == null)
{
lock (instanceLock)
{
if (m_Instance == null)
{
m_Instance = new PageData();
}
}
}
return m_Instance;
}
}
public void ReSync()
{
lock (instanceLock)
{
/* Setting to null to force the Instance to re-build */
m_Instance = null;
PageData pData = Instance;
}
}
thanks
This is slightly incorrect, yourif (m_Instance == null)should really be on the inside of the lock.
Sorry, didn’t spot that.
Nothing is built in that can make other clients abandon calls silently if you are already refreshing. Timeouts will generate an exception I think. Perhaps maintain a stale DateTime that you can check to avoid doing the refresh on queued callers.