All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neuro4j.workflow.guava.GuavaCachedWorkflowLoader Maven / Gradle / Ivy

The newest version!
package org.neuro4j.workflow.guava;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.neuro4j.workflow.common.FlowExecutionException;
import org.neuro4j.workflow.common.Workflow;
import org.neuro4j.workflow.loader.WorkflowLoader;

import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

/**
 * WorkflowLoader based on Guava Cache.
 *
 */
public class GuavaCachedWorkflowLoader implements WorkflowLoader{
	
	  private final WorkflowLoader delegate;

	  private final Cache cache;
	  

	public GuavaCachedWorkflowLoader(WorkflowLoader delegate, Cache cache) {
		super();
		this.delegate = delegate;
		this.cache = cache;
	}

	public static GuavaCachedWorkflowLoader cacheWithExpiration(final WorkflowLoader delegate, final long duration,
			final TimeUnit unit) {
		Cache cache = CacheBuilder.newBuilder().expireAfterAccess(duration, unit).build();
		return new GuavaCachedWorkflowLoader(delegate, cache);
	}


	@Override
	public Workflow load(final String name) throws FlowExecutionException {
		try {
			return cache.get(name, new Callable() {
				@Override
				public Workflow call() throws Exception {
					return delegate.load(name);
				}
			});
		} catch (ExecutionException e) {
			Throwables.propagateIfPossible(e.getCause(), FlowExecutionException.class);
			throw Throwables.propagate(e.getCause());
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy