Caching, the funny way
Join the DZone community and get the full member experience.
Join For Freeone of the most frustrating things in working with ravendb is that the client api is compatible with the 3.5 framework. that means that for a lot of things we either have to use conditional compilation or we have to forgo using the new stuff in 4.0.
case in point, we have the following issue:
the code in question currently looks like this:
this is the sort of code that simply begs to be used with concurrentdictionary. unfortunately, we can’t use that here, because of the 3.5 limitation. instead, i went with the usual, non thread safe, dictionary approach. i wanted to avoid locking, so i ended up with:
pretty neat, even if i say so myself. the fun part that without any locking, this is completely thread safe. the field itself is initialized to an empty dictionary in the constructor, of course, but that is the only thing that is happening outside this method. for that matter, i didn’t even bother to make the field volatile. the only thing that this relies on is that pointer writes are atomic.
how comes this works, and what assumptions am i making that makes this thing possible?
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments