KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > UpdateTimestampsCache


1 //$Id: UpdateTimestampsCache.java,v 1.9 2005/03/16 06:01:17 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Properties JavaDoc;
7 import java.util.Set JavaDoc;
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 /**
16  * Tracks the timestamps of the most recent updates to particular tables. It is
17  * important that the cache timeout of the underlying cache implementation be set
18  * to a higher value than the timeouts of any of the query caches. In fact, we
19  * recommend that the the underlying cache not be configured for expiry at all.
20  * Note, in particular, that an LRU cache expiry policy is never appropriate.
21  * @author Gavin King, Mikheil Kapanadze
22  */

23 public class UpdateTimestampsCache {
24
25     private static final Log log = LogFactory.getLog(UpdateTimestampsCache.class);
26
27     private Cache updateTimestamps;
28     private final String JavaDoc regionName;
29
30     public static final String JavaDoc REGION_NAME = UpdateTimestampsCache.class.getName();
31
32     public void clear() throws CacheException {
33         updateTimestamps.clear();
34     }
35
36     public UpdateTimestampsCache(Settings settings, Properties JavaDoc props)
37     throws HibernateException {
38         String JavaDoc 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 JavaDoc[] spaces) throws CacheException {
48         //TODO: to handle concurrent writes correctly, this should return a Lock to the client
49
Long JavaDoc ts = new Long JavaDoc( 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             //put() has nowait semantics, is this really appropriate?
53
//note that it needs to be async replication, never local or sync
54
updateTimestamps.put( spaces[i], ts );
55         }
56         //TODO: return new Lock(ts);
57
}
58
59      public synchronized void invalidate(Serializable JavaDoc[] spaces) throws CacheException {
60         //TODO: to handle concurrent writes correctly, the client should pass in a Lock
61
Long JavaDoc ts = new Long JavaDoc( updateTimestamps.nextTimestamp() );
62         //TODO: if lock.getTimestamp().equals(ts)
63
for ( int i=0; i<spaces.length; i++ ) {
64             if ( log.isDebugEnabled() ) log.debug("Invalidating space [" + spaces[i] + "], timestamp: " + ts);
65             //put() has nowait semantics, is this really appropriate?
66
//note that it needs to be async replication, never local or sync
67
updateTimestamps.put( spaces[i], ts );
68         }
69     }
70
71     public synchronized boolean isUpToDate(Set JavaDoc spaces, Long JavaDoc timestamp) throws HibernateException {
72         Iterator JavaDoc iter = spaces.iterator();
73         while ( iter.hasNext() ) {
74             Serializable JavaDoc space = (Serializable JavaDoc) iter.next();
75             Long JavaDoc lastUpdate = (Long JavaDoc) updateTimestamps.get(space);
76             if ( lastUpdate==null ) {
77                 //the last update timestamp was lost from the cache
78
//(or there were no updates since startup!)
79
//updateTimestamps.put( space, new Long( updateTimestamps.nextTimestamp() ) );
80
//result = false; // safer
81
}
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 JavaDoc 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 JavaDoc getRegionName() {
106         return regionName;
107     }
108     
109     public String JavaDoc toString() {
110         return "UpdateTimestampeCache";
111     }
112
113 }
114
Popular Tags