Well, you can think of the cache as a dependency - and pass the instance (possibly the same instance) to whatever you need. This is what I would do in a situation where I already used dependency injection.
This will make it easier to test those classes that need a cache - you can create a new cache for each test and not worry about clearing the existing one. You can also parallelize tests without worrying about two tests that have ruined each other's secrets. In addition, it makes the use of the cache more understandable.
(Regardless of whether you use the interface to represent the cache, since cdhowie offers are up to you, you donโt have to do this, although you would separate the classes from your dependencies more if you did. If your cache is very simple and you donโt mind using production implementation in tests, maybe you should not extract the interface.)
source share