我正在开发一个小项目来了解Google App Engine,该项目是Java并且有Customer对象,Customer的实例可以有策略.每个客户都在自己的实体组中,以便可以使用事务来修改客户.
该站点的主页是客户列表,当添加新客户时,将再次显示客户列表.
由于每个客户都在他们自己的实体组中,因此新添加的客户有时不会出现在新客户列表中,几秒钟后刷新客户列表并且客户将出现.删除客户时存在类似的问题,您删除了客户,但它在整个列表中显示几秒钟.据我所知,由于数据存储提供的最终一致性,因此可以在Google App Engine中实现这一点.
所以我试图通过使用memcache来存储最近添加或最近删除的客户来解决这个问题.我正在使用的代码如下.
public List<Customer> getCustomers() {
List<Customer> cachedCustomers = myCache.getCached();
List<Customer> recentlyDeleted = myCache.getDeleted();
// Calls the real datastore.
List<Customer> dbCustomers = customerDao.getCustomerList();
Set<Customer> allCustomers = new HashSet<Customer>();
// Add cached first as these are most the most up todate.
allCustomers.addAll(cachedCustomers);
allCustomers.addAll(dbCustomers);
allCustomers.removeAll(recentlyDeleted);
List<Customer> allList = new ArrayList<Customer>();
allList.addAll(allCustomers);
Collections.sort(allList);
return allList;
}
Run Code Online (Sandbox Code Playgroud)
我在这里问,因为我认为我正在采取的方式不会感觉到"正确"的方式,并希望听到那些知道更好的方法来解决最终一致性产生的问题的人.
我有一个字符串的源容器我想从源容器中删除与谓词匹配的任何字符串,并将它们添加到目标容器中.
remove_copy_if和其他算法只能重新排序容器中的元素,因此必须由erase成员函数跟进.我的书(Josuttis)说remove_copy_if在目标容器中的最后一个位置之后返回一个迭代器.因此,如果我只在目标容器中有一个迭代器,我erase该如何调用源容器?我已经尝试使用目标的大小来确定从源容器的末尾回去多远,但没有运气.我只提出了以下代码,但它会进行两次调用(remove_if和remove_copy_if).
有人能让我知道正确的方法吗?我确信两次线性调用不是这样做的方法.
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
class CPred : public unary_function<string, bool>
{
public:
CPred(const string& arString)
:mString(arString)
{
}
bool operator()(const string& arString) const
{
return (arString.find(mString) == std::string::npos);
}
private:
string mString;
};
int main()
{
vector<string> Strings;
vector<string> Container;
Strings.push_back("123");
Strings.push_back("145");
Strings.push_back("ABC");
Strings.push_back("167");
Strings.push_back("DEF");
cout << "Original list" << endl;
copy(Strings.begin(), Strings.end(),ostream_iterator<string>(cout,"\n"));
CPred Pred("1"); …Run Code Online (Sandbox Code Playgroud)