Java Code Examples for net.sf.ehcache.Ehcache#get()

The following examples show how to use net.sf.ehcache.Ehcache#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EhCacheFacade.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Retrieves an object stored under the given key from the cache specified in
 * the given caching model. The caching model should be an instance of
 * <code>{@link EhCacheCachingModel}</code>.
 *
 * @param key   the key of the cache entry
 * @param model the caching model
 * @return the object retrieved from the cache. Can be <code>null</code>.
 * @throws CacheNotFoundException if the cache specified in the given model cannot be found.
 * @throws CacheAccessException   wrapping any unexpected exception thrown by the cache.
 * @see AbstractCacheProviderFacade#onGetFromCache(Serializable,CachingModel)
 */
protected Object onGetFromCache(Serializable key, CachingModel model)
		throws CacheException {
	Ehcache cache = getCache(model);
	Object cachedObject = null;

	try {
		Element cacheElement = cache.get(key);
		if (cacheElement != null) {
			cachedObject = cacheElement.getValue();
		}

	} catch (Exception exception) {
		throw new CacheAccessException(exception);
	}

	return cachedObject;
}
 
Example 2
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public List<Route> listRouteWithParallel(String id) throws Exception {
	List<Route> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(Parallel.class);
	String cacheKey = ApplicationCache.concreteCacheKey(id, Parallel.class.getCanonicalName());
	Element element = cache.get(cacheKey);
	if (null != element) {
		list = (List<Route>) element.getObjectValue();
	} else {
		EntityManager em = this.entityManagerContainer().get(Route.class);
		Parallel parallel = this.get(id, Parallel.class);
		if (null != parallel) {
			CriteriaBuilder cb = em.getCriteriaBuilder();
			CriteriaQuery<Route> cq = cb.createQuery(Route.class);
			Root<Route> root = cq.from(Route.class);
			Predicate p = root.get(Route_.id).in(parallel.getRouteList());
			list = em.createQuery(cq.where(p).orderBy(cb.asc(root.get(Route_.orderNumber)))).getResultList();
			if (!list.isEmpty()) {
				cache.put(new Element(cacheKey, list));
			}
		}
	}
	return list;
}
 
Example 3
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends JpaObject> T pick(String flag, Class<T> clz) throws Exception {
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	T t = null;
	Element element = cache.get(flag);
	if (null != element) {
		if (null != element.getObjectValue()) {
			t = (T) element.getObjectValue();
		}
	} else {
		t = this.entityManagerContainer().flag(flag, clz);
		if (t != null) {
			this.entityManagerContainer().get(clz).detach(t);
		}
		cache.put(new Element(flag, t));
	}
	return t;
}
 
Example 4
Source File: AbstractFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends JpaObject> T pick(String flag, Class<T> clz, String... attributes) throws Exception {
	if (StringUtils.isEmpty(flag)) {
		return null;
	}
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	T t = null;
	Element element = cache.get(flag);
	if (null != element) {
		if (null != element.getObjectValue()) {
			t = (T) element.getObjectValue();
		}
	} else {
		t = this.entityManagerContainer().flag(flag, clz);
		if (t != null) {
			this.entityManagerContainer().get(clz).detach(t);
		}
		cache.put(new Element(flag, t));
	}
	return t;
}
 
Example 5
Source File: AbstractFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends JpaObject> T pick(String flag, Class<T> clz, String... attributes) throws Exception {
	if (StringUtils.isEmpty(flag)) {
		return null;
	}
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	T t = null;
	Element element = cache.get(flag);
	if (null != element) {
		if (null != element.getObjectValue()) {
			t = (T) element.getObjectValue();
		}
	} else {
		t = this.entityManagerContainer().flag(flag, clz);
		if (t != null) {
			this.entityManagerContainer().get(clz).detach(t);
		}
		cache.put(new Element(flag, t));
	}
	return t;
}
 
Example 6
Source File: CmsSiteFlowMngImpl.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public int freshCacheToDB(Ehcache cache) {
	int count = 0;
	List<String> list = cache.getKeys();
	for (String uuid : list) {
		Element element = cache.get(uuid);
		if (element == null) {
			return count;
		}
		CmsSiteFlow cmsSiteFlow = (CmsSiteFlow) element.getValue();
		if (cmsSiteFlow.getId() == null
				&& cmsSiteFlow.getSessionId() != null) {
			dao.save(cmsSiteFlow);
		}
	}
	return count;
}
 
Example 7
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends JpaObject> T get(String id, Class<T> clz) throws Exception {
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	T t = null;
	String cacheKey = id;
	Element element = cache.get(cacheKey);
	if (null != element) {
		t = (T) element.getObjectValue();
	} else {
		t = this.entityManagerContainer().find(id, clz);
		if (null != t) {
			cache.put(new Element(cacheKey, t));
		}
	}
	return t;
}
 
Example 8
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public Begin getBeginWithProcess(String id) throws Exception {
	Begin begin = null;
	Ehcache cache = ApplicationCache.instance().getCache(Begin.class);
	String cacheKey = id;
	Element element = cache.get(cacheKey);
	if (null != element) {
		begin = (Begin) element.getObjectValue();
	} else {
		EntityManager em = this.entityManagerContainer().get(Begin.class);
		Process process = this.get(id, Process.class);
		if (null != process) {
			CriteriaBuilder cb = em.getCriteriaBuilder();
			CriteriaQuery<Begin> cq = cb.createQuery(Begin.class);
			Root<Begin> root = cq.from(Begin.class);
			Predicate p = cb.equal(root.get(Begin_.process), process.getId());
			List<Begin> list = em.createQuery(cq.where(p)).setMaxResults(1).getResultList();
			if (!list.isEmpty()) {
				begin = list.get(0);
				cache.put(new Element(cacheKey, begin));
			}
		}
	}
	return begin;
}
 
Example 9
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public List<Route> listRouteWithChoice(String id) throws Exception {
	List<Route> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(Route.class);
	String cacheKey = ApplicationCache.concreteCacheKey(id, Choice.class.getCanonicalName());
	Element element = cache.get(cacheKey);
	if (null != element) {
		list = (List<Route>) element.getObjectValue();
	} else {
		EntityManager em = this.entityManagerContainer().get(Route.class);
		Choice choice = this.get(id, Choice.class);
		if (null != choice) {
			CriteriaBuilder cb = em.getCriteriaBuilder();
			CriteriaQuery<Route> cq = cb.createQuery(Route.class);
			Root<Route> root = cq.from(Route.class);
			Predicate p = root.get(Route_.id).in(choice.getRouteList());
			list = em.createQuery(cq.where(p).orderBy(cb.asc(root.get(Route_.orderNumber)))).getResultList();
			if (!list.isEmpty()) {
				cache.put(new Element(cacheKey, list));
			}
		}
	}
	return list;
}
 
Example 10
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public List<Route> listRouteWithManual(String id) throws Exception {
	List<Route> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(Manual.class);
	String cacheKey = ApplicationCache.concreteCacheKey(id, Manual.class.getCanonicalName());
	Element element = cache.get(cacheKey);
	if (null != element) {
		list = (List<Route>) element.getObjectValue();
	} else {
		EntityManager em = this.entityManagerContainer().get(Route.class);
		Manual manual = this.get(id, Manual.class);
		if (null != manual) {
			CriteriaBuilder cb = em.getCriteriaBuilder();
			CriteriaQuery<Route> cq = cb.createQuery(Route.class);
			Root<Route> root = cq.from(Route.class);
			Predicate p = root.get(Route_.id).in(manual.getRouteList());
			list = em.createQuery(cq.where(p).orderBy(cb.asc(root.get(Route_.orderNumber)))).getResultList();
			if (!list.isEmpty()) {
				cache.put(new Element(cacheKey, list));
			}
		}
	}
	return list;
}
 
Example 11
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends JpaObject> List<T> listWithProcess(Class<T> clz, Process process) throws Exception {
	List<T> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	String cacheKey = ApplicationCache.concreteCacheKey("listWithProcess", process.getId(), clz.getName());
	Element element = cache.get(cacheKey);
	if (null != element) {
		Object obj = element.getObjectValue();
		if (null != obj) {
			list = (List<T>) obj;
		}
	} else {
		EntityManager em = this.entityManagerContainer().get(clz);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<T> cq = cb.createQuery(clz);
		Root<T> root = cq.from(clz);
		Predicate p = cb.equal(root.get(Agent.process_FIELDNAME), process.getId());
		cq.select(root).where(p);
		List<T> os = em.createQuery(cq).getResultList();
		for (T t : os) {
			em.detach(t);
			list.add(t);
		}
		/* 将object改为unmodifiable */
		list = Collections.unmodifiableList(list);
		cache.put(new Element(cacheKey, list));
	}
	return list;
}
 
Example 12
Source File: ScriptFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public Script getScriptWithAppInfoWithUniqueName( String appId, String uniqueName ) throws Exception {
	Script script = null;
	try {
		Ehcache cache = ApplicationCache.instance().getCache( Script.class );
		String cacheKey = "script.getScriptWithAppInfoWithUniqueName." + appId + "." + uniqueName;
		Element element = cache.get( cacheKey );
		if (null != element) {
			script = (Script) element.getObjectValue();
		} else {
			EntityManager em = this.entityManagerContainer().get(Script.class);
			CriteriaBuilder cb = em.getCriteriaBuilder();
			CriteriaQuery<Script> cq = cb.createQuery(Script.class);
			Root<Script> root = cq.from(Script.class);
			Predicate p = cb.equal( root.get(Script_.name), uniqueName );
			p = cb.or( p, cb.equal( root.get(Script_.alias), uniqueName ));
			p = cb.or( p, cb.equal( root.get(Script_.id), uniqueName ));
			p = cb.and( p, cb.equal( root.get(Script_.appId), appId ));
			List<Script> list = em.createQuery( cq.where(p) ).setMaxResults(1).getResultList();
			if (!list.isEmpty()) {
				script = list.get(0);
				cache.put(new Element(cacheKey, script));
			}
		}
		return script;
	} catch (Exception e) {
		throw new Exception("getScriptWithAppInfoWithUniqueName error.", e);
	}
}
 
Example 13
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends JpaObject> List<T> listWithCategory(Class<T> clz, CategoryInfo categoryInfo) throws Exception {
	List<T> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	String cacheKey = "listWithCategory#" + categoryInfo.getId() + "#" + clz.getName();
	Element element = cache.get(cacheKey);
	if (null != element) {
		Object obj = element.getObjectValue();
		if (null != obj) {
			list = (List<T>) obj;
		}
	} else {
		EntityManager em = this.entityManagerContainer().get(clz);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<T> cq = cb.createQuery(clz);
		Root<T> root = cq.from(clz);
		Predicate p = cb.equal(root.get("categoryInfo"), categoryInfo.getId());
		cq.select(root).where(p);
		List<T> os = em.createQuery(cq).getResultList();
		for (T t : os) {
			em.detach(t);
			list.add(t);
		}
		/* 将object改为unmodifiable */
		list = Collections.unmodifiableList(list);
		cache.put(new Element(cacheKey, list));
	}
	return list;
}
 
Example 14
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<Mapping> listMappingEffectiveWithApplicationAndProcess(String application, String process)
		throws Exception {
	final List<Mapping> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(Mapping.class);
	String cacheKey = ApplicationCache.concreteCacheKey(application, process, Application.class.getName(),
			Process.class.getName());
	Element element = cache.get(cacheKey);
	if (null != element) {
		list.addAll((List<Mapping>) element.getObjectValue());
	} else {
		EntityManager em = this.entityManagerContainer().get(Mapping.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Mapping> cq = cb.createQuery(Mapping.class);
		Root<Mapping> root = cq.from(Mapping.class);
		Predicate p = cb.equal(root.get(Mapping_.enable), true);
		p = cb.and(p, cb.equal(root.get(Mapping_.application), application));
		p = cb.and(p, cb.or(cb.equal(root.get(Mapping_.process), process), cb.equal(root.get(Mapping_.process), ""),
				cb.isNull(root.get(Mapping_.process))));
		List<Mapping> os = em.createQuery(cq.where(p)).getResultList();
		os.stream().collect(Collectors.groupingBy(o -> {
			return o.getApplication() + o.getTableName();
		})).forEach((k, v) -> {
			list.add(v.stream().filter(i -> StringUtils.isNotEmpty(i.getProcess())).findFirst().orElse(v.get(0)));
		});
		if (!list.isEmpty()) {
			cache.put(new Element(cacheKey, list));
		}
	}
	return list;
}
 
Example 15
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public List<Script> listScriptNestedWithApplicationWithUniqueName(String applicationId, String uniqueName)
		throws Exception {
	List<Script> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(Script.class);
	String cacheKey = applicationId + "." + uniqueName;
	Element element = cache.get(cacheKey);
	if (null != element) {
		list = (List<Script>) element.getObjectValue();
	} else {
		List<String> names = new ArrayList<>();
		names.add(uniqueName);
		while (!names.isEmpty()) {
			List<String> loops = new ArrayList<>();
			for (String name : names) {
				Script o = this.getScriptWithApplicationWithUniqueName(applicationId, name);
				if ((null != o) && (!list.contains(o))) {
					list.add(o);
					loops.addAll(o.getDependScriptList());
				}
			}
			names = loops;
		}
		if (!list.isEmpty()) {
			Collections.reverse(list);
			cache.put(new Element(cacheKey, list));
		}
	}
	return list;
}
 
Example 16
Source File: QueryViewFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public QueryView pick(String flag, Application application, ExceptionWhen exceptionWhen) throws Exception {
	Ehcache cache = ApplicationCache.instance().getCache(QueryView.class);
	String cacheKey = flag + "#" + application.getId();
	Element element = cache.get(cacheKey);
	QueryView o = null;
	if (null != element) {
		if (null != element.getObjectValue()) {
			o = (QueryView) element.getObjectValue();
		}
	} else {
		EntityManager em = this.entityManagerContainer().get(QueryView.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<QueryView> cq = cb.createQuery(QueryView.class);
		Root<QueryView> root = cq.from(QueryView.class);
		Predicate p = cb.equal(root.get(QueryView_.application), application.getId());
		p = cb.and(p, cb.or(cb.equal(root.get(QueryView_.id), flag), cb.equal(root.get(QueryView_.alias), flag),
				cb.equal(root.get(QueryView_.name), flag)));
		cq.select(root).where(p);
		List<QueryView> list = em.createQuery(cq).getResultList();
		if (list.isEmpty()) {
			cache.put(new Element(cacheKey, o));
		} else if (list.size() == 1) {
			o = list.get(0);
			em.detach(o);
			cache.put(new Element(cacheKey, o));
		} else {
			throw new Exception("multiple queryView with flag:" + flag + ".");
		}
	}
	if (o == null && Objects.equals(ExceptionWhen.not_found, exceptionWhen)) {
		throw new Exception("can not find queryView with flag:" + flag + ".");
	}
	return o;
}
 
Example 17
Source File: ElementFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends JpaObject> List<T> listWithProcess(Class<T> clz, Process process) throws Exception {
	List<T> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(clz);
	String cacheKey = "listWithProcess#" + process.getId() + "#" + clz.getName();
	Element element = cache.get(cacheKey);
	if (null != element) {
		Object obj = element.getObjectValue();
		if (null != obj) {
			list = (List<T>) obj;
		}
	} else {
		EntityManager em = this.entityManagerContainer().get(clz);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<T> cq = cb.createQuery(clz);
		Root<T> root = cq.from(clz);
		Predicate p = cb.equal(root.get("process"), process.getId());
		cq.select(root).where(p);
		List<T> os = em.createQuery(cq).getResultList();
		for (T t : os) {
			em.detach(t);
			list.add(t);
		}
		/* 将object改为unmodifiable */
		list = Collections.unmodifiableList(list);
		cache.put(new Element(cacheKey, list));
	}
	return list;
}
 
Example 18
Source File: MultiMarkupEhCacheProvider.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Collection<T> getValues() {
    final Ehcache ehcache = (Ehcache) CACHE.getNativeCache();
    final List<T> values = new ArrayList<>();
    for (final Object key : ehcache.getKeys()) {
        final Element elem = ehcache.get(key);
        if (elem.getObjectValue() != null && !elem.isExpired()) {
           final T val = (T) elem.getObjectValue();
            values.add(val);
        }
    }
    return values;
}
 
Example 19
Source File: QueryStatFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public QueryStat pick(String flag, Application application, ExceptionWhen exceptionWhen) throws Exception {
	Ehcache cache = ApplicationCache.instance().getCache(QueryStat.class);
	String cacheKey = flag + "#" + application.getId();
	Element element = cache.get(cacheKey);
	QueryStat o = null;
	if (null != element) {
		if (null != element.getObjectValue()) {
			o = (QueryStat) element.getObjectValue();
		}
	} else {
		EntityManager em = this.entityManagerContainer().get(QueryStat.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<QueryStat> cq = cb.createQuery(QueryStat.class);
		Root<QueryStat> root = cq.from(QueryStat.class);
		Predicate p = cb.equal(root.get(QueryStat_.application), application.getId());
		p = cb.and(p, cb.or(cb.equal(root.get(QueryStat_.id), flag), cb.equal(root.get(QueryStat_.alias), flag),
				cb.equal(root.get(QueryStat_.name), flag)));
		cq.select(root).where(p);
		List<QueryStat> list = em.createQuery(cq).getResultList();
		if (list.isEmpty()) {
			cache.put(new Element(cacheKey, o));
		} else if (list.size() == 1) {
			o = list.get(0);
			em.detach(o);
			cache.put(new Element(cacheKey, o));
		} else {
			throw new Exception("multiple queryStat with flag:" + flag + ".");
		}
	}
	if (o == null && Objects.equals(ExceptionWhen.not_found, exceptionWhen)) {
		throw new Exception("can not find queryStat with flag:" + flag + ".");
	}
	return o;
}
 
Example 20
Source File: ScriptFactory.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public List<Script> listScriptNestedWithApplicationWithUniqueName(Application application, String uniqueName)
		throws Exception {
	List<Script> list = new ArrayList<>();
	Ehcache cache = ApplicationCache.instance().getCache(Script.class);
	String cacheKey = ApplicationCache.concreteCacheKey("listScriptNestedWithWithApplicationWithUniqueName",
			application.getId(), uniqueName);
	Element element = cache.get(cacheKey);
	if (null != element) {
		if (null != element.getObjectValue()) {
			list = (List<Script>) element.getObjectValue();
		}
	} else {
		List<String> names = new ArrayList<>();
		names.add(uniqueName);
		while (!names.isEmpty()) {
			List<String> loops = new ArrayList<>();
			for (String name : names) {
				Script o = this.getScriptWithApplicationWithUniqueName(application, name);
				if ((null != o) && (!list.contains(o))) {
					list.add(o);
					loops.addAll(o.getDependScriptList());
				}
			}
			names = loops;
		}
		if (!list.isEmpty()) {
			Collections.reverse(list);
			cache.put(new Element(cacheKey, list));
		}
	}
	return list;
}