當前位置: 首頁>>代碼示例>>Java>>正文


Java Cache.getOrDefault方法代碼示例

本文整理匯總了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);
	}
}
 
開發者ID:openwide-java,項目名稱:owsi-core-parent,代碼行數:22,代碼來源:InfinispanClusterServiceImpl.java

示例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;
	}
}
 
開發者ID:openwide-java,項目名稱:owsi-core-parent,代碼行數:70,代碼來源:InfinispanClusterServiceImpl.java


注:本文中的org.infinispan.Cache.getOrDefault方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。