I need to store and update List in MemoryCache. Every transaction hitting my WEB.API will add a record to this List, and every 1 min the list will be dumped into database and removed from cache.
My issue is concurrency and avoiding dirty reads/writes. I also need to write to this list very quickly so my transaction is not delayed.
// get list in cache
List<MyObj> stat = cache.Get<List<MyObj>>(key);
if (stat == null)
stat = new List<MyObj>() { new MyObj() { SomeKey = SomeVal } };
else
stat.Add(new MyObj() { SomeKey = SomeVal });
// by this time another thread could modify the cached object, so my current object is not good anymore
cache.Set<List<MyObj>>(key, stat);