Suppose I have a repository (ContactsRepository)
with method like getAllContacts(userId)
.
The (ContactsRepository)
fetch all data from server. After first call getAllContacts(userId)
method I want to cache this data. My question is where I should realize caching mechanism, in the same (ContactsRepository)
and when I invoke getAllContacts(userId)
method again the repository back my caching data or I need put the data into another place (maybe Repository).
You can use Repository to obtain the data either from the cache or from the database. if your Repository classes have update methods you can effectively invalidate the cache items as well.
You can thus encapsulate the access to cache within the Repository: http://martinfowler.com/eaaCatalog/repository.html
Another example to implement caching for Repository: http://ardalis.com/building-a-cachedrepository-via-strategy-pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With