防止与spymemcached冲突
我正在尝试使用spymemcached 2.6与更新同步,我发现了以下两种使用它的方法:
-
使用CASMutation定义CASMutator,这是一种相当侵入性的实现方式,让我们看一个例子:
公共列表<项目>; addAnItem(final Item newItem) 抛出异常 { // 这就是当我们在缓存中找到列表时修改列表的方式。 CASMutation<列表<项目>>突变 = new CASMutation
- >() {
// 仅当值实际存在时才会调用。
公共列表<项目> getNewValue(List
- 当前) {
// 如果您将存储指定为,则并非绝对必要
// LinkedList(我们的初始值不是),但我喜欢保留
// 无论如何,事情都是有用的,所以我要复制这个列表
// 第一的。
链接列表<项目> ll = new LinkedList
- (当前); // 如果列表已经“满”,则从末尾弹出一个。 if(ll.size() > 10) { ll.removeLast(); } // 首先添加我的。 ll.addFirst(newItem); 返回ll; } }; // 初始值——仅当没有列表存储时使用 // 密钥。 列表<项目>初始值=Collections.singletonList(newItem); // 将完成所有低级工作的 mutator。 CASMutor<列表<项目>> mutator = new CASMutor
- >(客户端,转码器);
// 这将返回成功存储在
// 缓存——或者是上面的初始列表,或者是一个变异的现有列表
// 一
return mutator.cas("myKey",initialValue,0,mutation);
}
- (当前); // 如果列表已经“满”,则从末尾弹出一个。 if(ll.size() > 10) { ll.removeLast(); } // 首先添加我的。 ll.addFirst(newItem); 返回ll; } }; // 初始值——仅当没有列表存储时使用 // 密钥。 列表<项目>初始值=Collections.singletonList(newItem); // 将完成所有低级工作的 mutator。 CASMutor<列表<项目>> mutator = new CASMutor
- 当前) {
// 如果您将存储指定为,则并非绝对必要
// LinkedList(我们的初始值不是),但我喜欢保留
// 无论如何,事情都是有用的,所以我要复制这个列表
// 第一的。
链接列表<项目> ll = new LinkedList
使用cas
方法
cas(String key, long casId, Object value)
或者在完成后
gets(String key, Transcoder<T> tc)
:第二个确实更多简单,我明白为什么我会使用 CASMutation... 我真的很高兴得到一些关于使用这个 couchbase 客户端的反馈。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CASMutor/CASMutation 捕捉最佳实践和工作流程,以便为您做正确的事情。
您的反例看起来更简单,因为您没有说出您实际上使用这些方法做什么。您上面发布的示例显示从 memcached 中提取一个列表,向其中添加一个新项目,有条件地从中删除一些项目,然后将其放回去。您发布的文本中至少有一半还需要您编写。
如果您不使用 CASMutator,您最终将重新发明它,而事情并非那么简单。这就是它今天为您所做的:
CASMutator/CASMutation captures the best practices and workflow so that the right thing is done for you.
Your counter-example looks more simple since you aren't saying what you'd actually do with those methods. The sample you posted above is showing a List being pulled out of memcached, a new item added to it, conditionally removing some items from it, and then putting it back. At least half of that text you posted you'd still need to write.
If you don't use CASMutator, you'll end up reinvent it, and that's not all that simple. This is what it does for you today: