📄 cachehelper.java
字号:
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Middleware LLC. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */package org.hibernate.cache.jbc2.util;import java.util.Collections;import java.util.Set;import org.hibernate.cache.CacheException;import org.jboss.cache.Cache;import org.jboss.cache.Fqn;import org.jboss.cache.InvocationContext;import org.jboss.cache.Node;import org.jboss.cache.config.Configuration;import org.jboss.cache.config.Option;import org.jboss.cache.lock.TimeoutException;import org.jboss.cache.optimistic.DataVersion;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Helper for dealing with JBossCache {@link Configuration.CacheMode}. * * @author Steve Ebersole * @author Brian Stansberry */public class CacheHelper { /** Key under which items are cached */ public static final String ITEM = "item"; /** Key and value used in a hack to create region root nodes */ public static final String DUMMY = "dummy"; private static final Logger log = LoggerFactory.getLogger(CacheHelper.class); /** * Disallow external instantiation of CacheHelper. */ private CacheHelper() { } /** * Is this cache participating in a cluster with invalidation? * * @param cache * The cache to check. * @return True if the cache is configured for synchronous/asynchronous * invalidation; false otherwise. */ public static boolean isClusteredInvalidation(Cache cache) { return isClusteredInvalidation(cache.getConfiguration().getCacheMode()); } /** * Does this cache mode indicate clustered invalidation? * * @param cacheMode * The cache to check * @return True if the cache mode is confiogured for * synchronous/asynchronous invalidation; false otherwise. */ public static boolean isClusteredInvalidation(Configuration.CacheMode cacheMode) { return cacheMode == Configuration.CacheMode.INVALIDATION_ASYNC || cacheMode == Configuration.CacheMode.INVALIDATION_SYNC; } /** * Is this cache participating in a cluster with replication? * * @param cache * The cache to check. * @return True if the cache is configured for synchronous/asynchronous * invalidation; false otherwise. */ public static boolean isClusteredReplication(Cache cache) { return isClusteredReplication(cache.getConfiguration().getCacheMode()); } /** * Does this cache mode indicate clustered replication? * * @param cacheMode * The cache to check * @return True if the cache mode is confiogured for * synchronous/asynchronous invalidation; false otherwise. */ public static boolean isClusteredReplication(Configuration.CacheMode cacheMode) { return cacheMode == Configuration.CacheMode.REPL_ASYNC || cacheMode == Configuration.CacheMode.REPL_SYNC; } public static boolean isSynchronous(Cache cache) { return isSynchronous(cache.getConfiguration().getCacheMode()); } public static boolean isSynchronous(Configuration.CacheMode cacheMode) { return cacheMode == Configuration.CacheMode.REPL_SYNC || cacheMode == Configuration.CacheMode.INVALIDATION_SYNC; } public static Set getChildrenNames(Cache cache, Fqn fqn) { Node node = cache.getRoot().getChild(fqn); return (node != null) ? node.getChildrenNames() : Collections.emptySet(); } /** * Builds an {@link Fqn} from <code>region</code> and <code>key</code> * and performs a JBoss Cache <code>get(Fqn, Object)</code>, wrapping any * exception in a {@link CacheException}. * * @param cache * the cache to invoke on * @param region * base Fqn for the cache region * @param key * specific key to append to the <code>region</code> to form * the full Fqn */ public static Object get(Cache cache, Fqn region, Object key) throws CacheException { try { return cache.get(new Fqn(region, key), ITEM); } catch (Exception e) { throw new CacheException(e); } } /** * Builds an {@link Fqn} from <code>region</code> and <code>key</code> * and performs a JBoss Cache <code>get(Fqn, Object)</code>, wrapping any * exception in a {@link CacheException}. * * @param cache * the cache to invoke on * @param region * base Fqn for the cache region * @param key * specific key to append to the <code>region</code> to form * the full Fqn */ public static Object getAllowingTimeout(Cache cache, Fqn region, Object key) throws CacheException { try { return cache.get(new Fqn(region, key), ITEM); } catch (TimeoutException ignored) { // ignore it return null; } catch (Exception e) { throw new CacheException(e); } } /** * Builds an {@link Fqn} from <code>region</code> and <code>key</code> * and performs a JBoss Cache <code>put(Object, Object)</code>, wrapping * any exception in a {@link CacheException}. * * @param cache * the cache to invoke on * @param region * base Fqn for the cache region * @param key * specific key to append to the <code>region</code> to form * the full Fqn * @param value * data to store in the cache node */ public static void put(Cache cache, Fqn region, Object key, Object value) throws CacheException { put(cache, region, key, value, null); } /** * Builds an {@link Fqn} from <code>region</code> and <code>key</code> * and performs a JBoss Cache <code>put(Object, Object)</code>, wrapping * any exception in a {@link CacheException}. * * @param cache * the cache to invoke on * @param region * base Fqn for the cache region * @param key * specific key to append to the <code>region</code> to form * the full Fqn * @param value * data to store in the cache node * @param option * invocation Option to set for this invocation. May be * <code>null</code>. */ public static void put(Cache cache, Fqn region, Object key, Object value, Option option) throws CacheException { try { setInvocationOption(cache, option); cache.put(new Fqn(region, key), ITEM, value); } catch (Exception e) { throw new CacheException(e); } } /** * Builds an {@link Fqn} from <code>region</code> and <code>key</code> * and performs a JBoss Cache <code>put(Object, Object)</code>, ignoring any * {@link TimeoutException} and wrapping any other exception in a {@link CacheException}. * * @param cache * the cache to invoke on * @param region * base Fqn for the cache region * @param key * specific key to append to the <code>region</code> to form * the full Fqn * @param value * data to store in the cache node * @param option * invocation Option to set for this invocation. May be * <code>null</code>. */ public static void putAllowingTimeout(Cache cache, Fqn region, Object key, Object value, Option option) throws CacheException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -