问题描述
我的目标是在C ++中实现存储库模式。有几个定义,我的反对意见是:
class Person { std::string name,int age };
class PersonRepository { Person& getPersonByName(std::string name); }
在我的代码中,我希望能够修改此人,因此无需调用任何更新函数。
Person person = personRepo.getPersonByName("Leon")
// This change will be updated automatically in the database
person.age = 20
我的想法是将对象保留在std :: vector中,以便在上述代码示例中返回引用。
class PersonRepository {
Person& getPersonByName(std::string name) {
if(person is in cache)
return person in cache
retrieve person from database
add to cache
return from cache
}
std::vector<Person> personCache;
}
在某些时候,缓存必须同步回数据库或上载到服务器(这并不重要)。 但是,存储库类显然不知道何时(或什至)人员对象已更改。那么,什么应该触发从缓存到数据库/服务器的同步呢?
我想到的一个想法是将它做成有限大小的fifo。因此,一旦缓存已满,对象就会被同步回去。bbt我不确定如何跟踪这些人对象在外面不再被使用。
如果有任何建议,我将非常高兴!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)