updatetimestampscache.java

来自「用Java实现的23个常用设计模式源代码」· Java 代码 · 共 78 行

JAVA
78
字号
//$Id: UpdateTimestampsCache.java,v 1.1.2.3 2003/11/29 07:55:15 oneovthafew Exp $package net.sf.hibernate.cache;import java.io.Serializable;import java.util.Iterator;import java.util.Properties;import java.util.Set;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.hibernate.HibernateException;/** * Tracks the timestamps of the most recent updates to particular tables. It is * important that the cache timeout of the underlying cache implementation be set * to a higher value than the timeouts of any of the query caches. In fact, we  * recommend that the the underlying cache not be configured for expiry at all. * Note, in particular, that an LRU cache expiry policy is never appropriate. * @author Gavin King, Mikheil Kapanadze */public class UpdateTimestampsCache {		private static final Log log = LogFactory.getLog(UpdateTimestampsCache.class);	private Cache updateTimestamps;		public static final String REGION_NAME = UpdateTimestampsCache.class.getName();	public void clear() throws CacheException {		updateTimestamps.clear();	}	public UpdateTimestampsCache(CacheProvider provider, Properties props) throws HibernateException {		log.info("starting update timestamps cache at region: " + REGION_NAME);		this.updateTimestamps = provider.buildCache(REGION_NAME, props);	}	public synchronized void preinvalidate(Serializable[] spaces) throws CacheException {		Long ts = new Long( updateTimestamps.nextTimestamp() + updateTimestamps.getTimeout() );		for ( int i=0; i<spaces.length; i++ ) updateTimestamps.put( spaces[i], ts );	}	 public synchronized void invalidate(Serializable[] spaces) throws CacheException {		Long ts = new Long( updateTimestamps.nextTimestamp() );		for ( int i=0; i<spaces.length; i++ ) updateTimestamps.put( spaces[i], ts );	}	public synchronized boolean isUpToDate(Set spaces, Long timestamp) throws HibernateException {		Iterator iter = spaces.iterator();		boolean result = true;		while ( iter.hasNext() ) {			Serializable space = (Serializable) iter.next();			Long lastUpdate = (Long) updateTimestamps.get(space);			if ( lastUpdate==null ) {				//the last update timestamp was lost from the cache				//(or there were no updates since startup!)				updateTimestamps.put( space, new Long( updateTimestamps.nextTimestamp() ) );				result = false; // safer			}			else {				if ( lastUpdate.longValue() > timestamp.longValue() ) result = false;			}		}		return result;	}	public void destroy() {		try {			updateTimestamps.destroy();		}		catch (Exception e) {			log.warn("could not destroy UpdateTimestamps cache", e);		}	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?