KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: NonstrictReadWriteCache.java,v 1.8 2005/02/12 07:19:08 steveebersole Exp $
2
package org.hibernate.cache;
3
4 import java.util.Comparator JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 /**
10  * Caches data that is sometimes updated without ever locking the cache.
11  * If concurrent access to an item is possible, this concurrency strategy
12  * makes no guarantee that the item returned from the cache is the latest
13  * version available in the database. Configure your cache timeout accordingly!
14  * This is an "asynchronous" concurrency strategy.
15  *
16  * @see ReadWriteCache for a much stricter algorithm
17  * @author Gavin King
18  */

19 public class NonstrictReadWriteCache implements CacheConcurrencyStrategy {
20
21     private Cache cache;
22
23     private static final Log log = LogFactory.getLog(NonstrictReadWriteCache.class);
24
25     public NonstrictReadWriteCache() {}
26
27     public void setCache(Cache cache) {
28         this.cache=cache;
29     }
30
31     public Cache getCache() {
32         return cache;
33     }
34
35     /**
36      * Get the most recent version, if available.
37      */

38     public Object JavaDoc get(Object JavaDoc key, long txTimestamp) throws CacheException {
39         if ( log.isDebugEnabled() ) log.debug("Cache lookup: " + key);
40
41         Object JavaDoc result = cache.get(key);
42         if ( result!=null ) {
43             log.debug("Cache hit");
44         }
45         else {
46             log.debug("Cache miss");
47         }
48         return result;
49     }
50
51     /**
52      * Add an item to the cache.
53      */

54     public boolean put(
55             Object JavaDoc key,
56             Object JavaDoc value,
57             long txTimestamp,
58             Object JavaDoc version,
59             Comparator JavaDoc versionComparator,
60             boolean minimalPut)
61     throws CacheException {
62         if ( minimalPut && cache.get(key)!=null ) {
63             if ( log.isDebugEnabled() ) log.debug("item already cached: " + key);
64             return false;
65         }
66         if ( log.isDebugEnabled() ) log.debug("Caching: " + key);
67         
68         cache.put(key, value);
69         return true;
70
71     }
72
73     /**
74      * Do nothing.
75      * @return null, no lock
76      */

77     public SoftLock lock(Object JavaDoc key, Object JavaDoc version) throws CacheException {
78         return null;
79     }
80
81     public void remove(Object JavaDoc key) throws CacheException {
82         if ( log.isDebugEnabled() ) log.debug("Removing: " + key);
83         cache.remove(key);
84     }
85
86     public void clear() throws CacheException {
87         if ( log.isDebugEnabled() ) log.debug("Clearing");
88         cache.clear();
89     }
90
91     public void destroy() {
92         try {
93             cache.destroy();
94         }
95         catch (Exception JavaDoc e) {
96             log.warn("could not destroy cache", e);
97         }
98     }
99
100     /**
101      * Invalidate the item
102      */

103     public void evict(Object JavaDoc key) throws CacheException {
104         if ( log.isDebugEnabled() ) log.debug("Invalidating: " + key);
105
106         cache.remove(key);
107     }
108
109     /**
110      * Invalidate the item
111      */

112     public boolean update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
113         evict(key);
114         return false;
115     }
116
117     /**
118      * Do nothing.
119      */

120     public boolean insert(Object JavaDoc key, Object JavaDoc value) throws CacheException {
121         return false;
122     }
123
124     /**
125      * Invalidate the item (again, for safety).
126      */

127     public void release(Object JavaDoc key, SoftLock lock) throws CacheException {
128         if ( log.isDebugEnabled() ) log.debug("Invalidating (again): " + key);
129
130         cache.remove(key);
131     }
132
133     /**
134      * Invalidate the item (again, for safety).
135      */

136     public boolean afterUpdate(Object JavaDoc key, Object JavaDoc value, Object JavaDoc version, SoftLock lock) throws CacheException {
137         release(key, lock);
138         return false;
139     }
140
141     /**
142      * Do nothing.
143      */

144     public boolean afterInsert(Object JavaDoc key, Object JavaDoc value, Object JavaDoc version) throws CacheException {
145         //if ( log.isDebugEnabled() ) log.debug("Adding new: " + key);
146

147         //cache.put(key, value);
148
return false;
149     }
150
151     public String JavaDoc getRegionName() {
152         return cache.getRegionName();
153     }
154     
155     public String JavaDoc toString() {
156         return cache + "(nonstrict-read-write)";
157     }
158 }
159
Popular Tags