1 package org.hibernate.cache; 3 4 import java.io.Serializable ; 5 import java.util.Iterator ; 6 import java.util.Properties ; 7 import java.util.Set ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import org.hibernate.HibernateException; 13 import org.hibernate.cfg.Settings; 14 15 23 public class UpdateTimestampsCache { 24 25 private static final Log log = LogFactory.getLog(UpdateTimestampsCache.class); 26 27 private Cache updateTimestamps; 28 private final String regionName; 29 30 public static final String REGION_NAME = UpdateTimestampsCache.class.getName(); 31 32 public void clear() throws CacheException { 33 updateTimestamps.clear(); 34 } 35 36 public UpdateTimestampsCache(Settings settings, Properties props) 37 throws HibernateException { 38 String prefix = settings.getCacheRegionPrefix(); 39 40 regionName = prefix==null ? 41 REGION_NAME : 42 prefix + '.' + REGION_NAME; 43 log.info("starting update timestamps cache at region: " + regionName); 44 this.updateTimestamps = settings.getCacheProvider().buildCache(regionName, props); 45 } 46 47 public synchronized void preinvalidate(Serializable [] spaces) throws CacheException { 48 Long ts = new Long ( updateTimestamps.nextTimestamp() + updateTimestamps.getTimeout() ); 50 for ( int i=0; i<spaces.length; i++ ) { 51 if ( log.isDebugEnabled() ) log.debug("Pre-invalidating space [" + spaces[i] + "]"); 52 updateTimestamps.put( spaces[i], ts ); 55 } 56 } 58 59 public synchronized void invalidate(Serializable [] spaces) throws CacheException { 60 Long ts = new Long ( updateTimestamps.nextTimestamp() ); 62 for ( int i=0; i<spaces.length; i++ ) { 64 if ( log.isDebugEnabled() ) log.debug("Invalidating space [" + spaces[i] + "], timestamp: " + ts); 65 updateTimestamps.put( spaces[i], ts ); 68 } 69 } 70 71 public synchronized boolean isUpToDate(Set spaces, Long timestamp) throws HibernateException { 72 Iterator iter = spaces.iterator(); 73 while ( iter.hasNext() ) { 74 Serializable space = (Serializable ) iter.next(); 75 Long lastUpdate = (Long ) updateTimestamps.get(space); 76 if ( lastUpdate==null ) { 77 } 82 else { 83 if ( log.isDebugEnabled() ) { 84 log.debug("[" + space + "] last update timstamp: " + lastUpdate + ", result set timestamp: " + timestamp ); 85 } 86 if ( lastUpdate.longValue() >= timestamp.longValue() ) return false; 87 } 88 } 89 return true; 90 } 91 92 public void destroy() { 93 try { 94 updateTimestamps.destroy(); 95 } 96 catch (Exception e) { 97 log.warn("could not destroy UpdateTimestamps cache", e); 98 } 99 } 100 101 public Cache getCache() { 102 return updateTimestamps; 103 } 104 105 public String getRegionName() { 106 return regionName; 107 } 108 109 public String toString() { 110 return "UpdateTimestampeCache"; 111 } 112 113 } 114 | Popular Tags |