Implementing a cache with ReaderWriterLockSlim

Caching is a common technique that is being used in many applications to increase performance and efficiency. Usually, reading from a cache occurs more often than writing operation, and the number of cache readers is higher that the number of writers.

In this particular case, there is no sense in using an exclusive lock preventing other threads from reading another cache value. There is a built-in synchronization object that has exactly this behavior, and it is called ReaderWriterLockSlim.

Note

There are several classes in the .NET Framework inside the System.Threading namespace, whose names end with Slim. It is usually more efficient and lightweight to implement the corresponding classes without Slim ...

Get Mastering C# Concurrency now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.