In one of the applications I’m building I wanted to abstract away the usage of the .NET MemoryCache behind a nice clean interface:
Implementing most parts of this interface wasn’t hard but I struggled with the Clear method. There is no easy way to just clear all items from the MemoryCache. I tried the Trim() method but it had not the desired effect. I was able to solve it by disposing the existing cache instance and creating a new one behind the scenes. Here is the complete implementation: