KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: TransactionalCache.java,v 1.10 2005/04/21 07:57:19 oneovthafew 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  * Support for fully transactional cache implementations like
11  * JBoss TreeCache. Note that this might be a less scalable
12  * concurrency strategy than <tt>ReadWriteCache</tt>. This is
13  * a "synchronous" concurrency strategy.
14  *
15  * @author Gavin King
16  */

17 public class TransactionalCache implements CacheConcurrencyStrategy {
18     
19     private static final Log log = LogFactory.getLog(TransactionalCache.class);
20
21     private Cache cache;
22
23     public String JavaDoc getRegionName() {
24         return cache.getRegionName();
25     }
26     
27     public Object JavaDoc get(Object JavaDoc key, long txTimestamp) throws CacheException {
28         if ( log.isDebugEnabled() ) log.debug("cache lookup: " + key);
29         Object JavaDoc result = cache.read(key);
30         if ( log.isDebugEnabled() ) {
31             log.debug( result==null ? "cache miss" : "cache hit" );
32         }
33         return result;
34     }
35
36     public boolean put(
37             Object JavaDoc key,
38             Object JavaDoc value,
39             long txTimestamp,
40             Object JavaDoc version,
41             Comparator JavaDoc versionComparator,
42             boolean minimalPut)
43     throws CacheException {
44         
45         if ( minimalPut && cache.read(key)!=null ) {
46             if ( log.isDebugEnabled() ) log.debug("item already cached: " + key);
47             return false;
48         }
49         if ( log.isDebugEnabled() ) log.debug("caching: " + key);
50         cache.put(key, value);
51         return true;
52     }
53
54     /**
55      * Do nothing, returning null.
56      */

57     public SoftLock lock(Object JavaDoc key, Object JavaDoc version) throws CacheException {
58         //noop
59
return null;
60     }
61
62     /**
63      * Do nothing.
64      */

65     public void release(Object JavaDoc key, SoftLock clientLock) throws CacheException {
66         //noop
67
}
68
69     public boolean update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
70         if ( log.isDebugEnabled() ) log.debug("updating: " + key);
71         cache.update(key, value);
72         return true;
73     }
74
75     public boolean insert(Object JavaDoc key, Object JavaDoc value) throws CacheException {
76         if ( log.isDebugEnabled() ) log.debug("inserting: " + key);
77         cache.update(key, value);
78         return true;
79     }
80
81     public void evict(Object JavaDoc key) throws CacheException {
82         cache.remove(key);
83     }
84
85     public void remove(Object JavaDoc key) throws CacheException {
86         if ( log.isDebugEnabled() ) log.debug("removing: " + key);
87         cache.remove(key);
88     }
89
90     public void clear() throws CacheException {
91         log.debug("clearing");
92         cache.clear();
93     }
94
95     public void destroy() {
96         try {
97             cache.destroy();
98         }
99         catch (Exception JavaDoc e) {
100             log.warn("could not destroy cache", e);
101         }
102     }
103
104     public void setCache(Cache cache) {
105         this.cache = cache;
106     }
107
108     public Cache getCache() {
109         return cache;
110     }
111
112     /**
113      * Do nothing.
114      */

115     public boolean afterInsert(Object JavaDoc key, Object JavaDoc value, Object JavaDoc version)
116     throws CacheException {
117         return false;
118     }
119
120     /**
121      * Do nothing.
122      */

123     public boolean afterUpdate(Object JavaDoc key, Object JavaDoc value, Object JavaDoc version, SoftLock clientLock)
124     throws CacheException {
125         return false;
126     }
127     
128     public String JavaDoc toString() {
129         return cache + "(transactional)";
130     }
131
132 }
133
Popular Tags