假设我有一个类似(ContactsRepository)的存储库getAllContacts(userId)(ContactsRepository)从服务器获取所有数据。第一次调用getAllContacts(userId)方法后,我想缓存此数据。我的问题是,在同一(ContactsRepository)中应该在哪里实现缓存机制,并且当我再次调用getAllContacts(userId)方法时,存储库会返回我的缓存数据,或者我需要将数据放到另一个地方(也许是存储库)。

最佳答案

您可以使用存储库从缓存或数据库中获取数据。如果您的存储库类具有更新方法,那么您也可以有效地使缓存项无效。

因此,您可以将对缓存的访问封装在存储库中:
http://martinfowler.com/eaaCatalog/repository.html

为存储库实现缓存的另一个示例:
http://ardalis.com/building-a-cachedrepository-via-strategy-pattern

关于oop - DDD : caching aggregate objects that received from sever,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13155793/

10-11 14:41
查看更多