本文整理匯總了Java中org.infinispan.Cache.getOrDefault方法的典型用法代碼示例。如果您正苦於以下問題:Java Cache.getOrDefault方法的具體用法?Java Cache.getOrDefault怎麽用?Java Cache.getOrDefault使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.infinispan.Cache
的用法示例。
在下文中一共展示了Cache.getOrDefault方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: filterPriorityQueue
import org.infinispan.Cache; //導入方法依賴的package包/類
private void filterPriorityQueue(IPriorityQueue priorityQueue, Predicate<IAttribution> attributionPredicate) {
Cache<IPriorityQueue, List<IAttribution>> cache = getPriorityQueuesCache();
boolean commit = false;
if (!cache.getAdvancedCache().startBatch()) {
LOGGER.error("Batch attempt on {} failed", priorityQueue);
}
try {
// get lock values
if (!cache.getAdvancedCache().lock(priorityQueue)) {
LOGGER.error("Lock attempt on {} failed", priorityQueue);
commit = false;
return;
}
List<IAttribution> values = cache.getOrDefault(priorityQueue, Lists.<IAttribution> newArrayList());
List<IAttribution> newValues = Lists.newArrayList(Collections2.filter(values, attributionPredicate));
cache.put(priorityQueue, newValues);
commit = true;
} finally {
cache.endBatch(commit);
}
}
示例2: doWithLockPriority
import org.infinispan.Cache; //導入方法依賴的package包/類
@Override
public boolean doWithLockPriority(ILockRequest lockRequest, Runnable runnable) throws ExecutionException {
if (!isClusterActive()) {
return false;
}
Cache<IPriorityQueue, List<IAttribution>> cache = getPriorityQueuesCache();
boolean commit = true;
boolean priorityAllowed = true;
if (!cache.startBatch()) {
LOGGER.error("Batch attempt on {} failed", lockRequest.getPriorityQueue());
}
try {
// get lock values
if (!cache.getAdvancedCache().lock(lockRequest.getPriorityQueue())) {
LOGGER.error("Lock attempt on {} failed", lockRequest.getPriorityQueue());
commit = false;
return false;
}
List<IAttribution> values = cache.getOrDefault(lockRequest.getPriorityQueue(),
Lists.<IAttribution> newArrayList());
// check if a slot is already kept
boolean prioritySlotFound = false;
for (IAttribution attribution : values) {
if (attribution.match(getAddress())) {
prioritySlotFound = true;
}
}
// if no slot, add it
if (!prioritySlotFound) {
// get a priority slot if absent
values.add(Attribution.from(getAddress(), new Date()));
cache.put(lockRequest.getPriorityQueue(), values);
}
// if slot is first, we can do our job
if (values.size() > 0 && values.get(0).match(getAddress())) {
// priority is allowed (first slot taken)
// allow second phase and remove priority slot
priorityAllowed = true;
} else {
// priority is not allowed, retry later when it is our slot turn
priorityAllowed = false;
}
} finally {
cache.endBatch(commit);
}
if (priorityAllowed) {
// return doWithLock result (true - job done ; false - job not launch)
try {
return doWithLock(lockRequest.getLock(), runnable);
} finally {
// get rid of this node slot
filterPriorityQueue(lockRequest.getPriorityQueue(),
new Predicate<IAttribution>() {
@Override
public boolean apply(IAttribution input) {
// keep all attribution of other nodes
return !input.match(getAddress());
}
});
}
} else {
return false;
}
}