Reputation: 207
I have product repository. And I want to use redis as cache. And I create cache repo.
When I want to get product. First I go cache repo if not exist I query main database . If product exists in There. I write to cache and return.
Option 1) I get cache repository in product repository via DI and use in there.
Option 2) I get cache repository in application layer in command handler with product repository and I use both separetly
Upvotes: 4
Views: 4002
Reputation: 2850
It seems to me that you are driven by technical requirements (i.e usage of Redis) and not business requirement (i.e why do you need caching ? performance issue, latency ?).
But, to sum up a great post from another thread in SO: Which layer should I implement caching of lookup data from database in a DDD application?, you have the following options:
Either way, one of the most common approach is to use the pattern proxy, where the method call will be intercepted first by the proxy, whom role is to send data from the cache if it already have the data. Otherwise delegate the call to the original object.
Upvotes: 8