防止与spymemcached冲突
|
我正在尝试将spymemcached 2.6与更新同步使用,我发现了以下两种使用方式:
使用CASMutation定义CASMutator,这是一种非常侵入性的实现方式,请看一个示例:
public List<Item> addAnItem(final Item newItem) throws Exception {
// This is how we modify a list when we find one in the cache.
CASMutation<List<Item>> mutation = new CASMutation<List<Item>>() {
// This is only invoked when a value actually exists.
public List<Item> getNewValue(List<Item> current) {
// Not strictly necessary if you specify the storage as
// LinkedList (our initial value isn\'t), but I like to keep
// things functional anyway, so I\'m going to copy this list
// first.
LinkedList<Item> ll = new LinkedList<Item>(current);
// If the list is already \"full\", pop one off the end.
if(ll.size() > 10) {
ll.removeLast();
}
// Add mine first.
ll.addFirst(newItem);
return ll;
}
};
// The initial value -- only used when there\'s no list stored under
// the key.
List<Item> initialValue=Collections.singletonList(newItem);
// The mutator who\'ll do all the low-level stuff.
CASMutator<List<Item>> mutator = new CASMutator<List<Item>>(client, transcoder);
// This returns whatever value was successfully stored within the
// cache -- either the initial list as above, or a mutated existing
// one
return mutator.cas(\"myKey\", initialValue, 0, mutation);
}
或使用cas
方法
cas(String key, long casId, Object value)
完成后:
gets(String key, Transcoder<T> tc)
第二个实际上更简单,我理解为什么要使用CASMutation ...
我真的很高兴收到有关此bedbase客户端使用的反馈。
没有找到相关结果
已邀请:
1 个回复
葛瞎说漓