I have cache implemented and this cache needs to be updated after certain intervals. Creating or updating cache involves calling lot many external APIs and may be little slower. My cache is a simple collection. I have made it static so that all the request refer to same instance. The problem arises when I am updating the cache. Before I put the new data I clear the cache. I simulated this scenario, where in I put some delay just after clearing the data and before updating it with new data. I get the obvious exception of no sequence found. How to handle this scenario. I guess this is very likely situation one would get into in multi threaded application.
I have explored ConcurrentBag, SynchronizedCollection and ReaderWriterLockSlim. Nothing seems to be working (or may be I am not using them rightly) My expectation: The reader thread should use the older version of cache while its being updated and new version right after it