eren arslan
eren arslan

Reputation: 207

DDD aggregate repository and caching repository

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

Answers (1)

RUARO Thibault
RUARO Thibault

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:

  • Manage the cache in the Application layer, directly in the Application Service. This way, you have full control whether you want to to use the cache or not for such query/command
  • Hide the cache in the repository. But here every clients of your repository will use the cache, and that is something maybe you want to have control over.

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

Related Questions