Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who is responsible for handling the logic of data-caching and in-memory storages?

In a web service, when we put the responsibility of holding data to repositories, should we also put the responsibility of caching to them? Or it's responsibility of another part?

Also, what about the situations when we use in-memory storages as a helper? for example when we counting number of posts' visits per day and saving them eventually in database, Should it handle on Repository?

like image 532
Amin Shojaei Avatar asked Oct 28 '25 14:10

Amin Shojaei


1 Answers

I think the decorator pattern works well for cached repository. The main idea is to separate caching and storage logic. So it will be correspond to Single responsibility principle.

enter image description here

What about statistic helper, it depends on the context. It can be:

  • Also decorator, pass requests through itself, and implements IRepository interface:

repo = new StatHelper(new CacheRepo(new DbRepo))

  • Separate tier with its own interface getting IRepository as a dependency.
  • Logic can be implemented inside CacheRepository so as not to inflate the number of classes
like image 145
alex_noname Avatar answered Oct 30 '25 14:10

alex_noname



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!