~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/cache/NonstrictReadWriteCache.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: NonstrictReadWriteCache.java 9278 2006-02-13 16:57:22Z steveebersole $
 
2
package org.hibernate.cache;
 
3
 
 
4
import java.util.Comparator;
 
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
 * @author Gavin King
 
17
 * @see ReadWriteCache for a much stricter algorithm
 
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
 
 
28
        public void setCache(Cache cache) {
 
29
                this.cache = cache;
 
30
        }
 
31
 
 
32
        public Cache getCache() {
 
33
                return cache;
 
34
        }
 
35
 
 
36
        /**
 
37
         * Get the most recent version, if available.
 
38
         */
 
39
        public Object get(Object key, long txTimestamp) throws CacheException {
 
40
                if ( log.isDebugEnabled() ) {
 
41
                        log.debug( "Cache lookup: " + key );
 
42
                }
 
43
 
 
44
                Object result = cache.get( key );
 
45
                if ( result != null ) {
 
46
                        log.debug( "Cache hit" );
 
47
                }
 
48
                else {
 
49
                        log.debug( "Cache miss" );
 
50
                }
 
51
                return result;
 
52
        }
 
53
 
 
54
        /**
 
55
         * Add an item to the cache.
 
56
         */
 
57
        public boolean put(
 
58
                        Object key,
 
59
                Object value,
 
60
                long txTimestamp,
 
61
                Object version,
 
62
                Comparator versionComparator,
 
63
                boolean minimalPut) throws CacheException {
 
64
                if ( minimalPut && cache.get( key ) != null ) {
 
65
                        if ( log.isDebugEnabled() ) {
 
66
                                log.debug( "item already cached: " + key );
 
67
                        }
 
68
                        return false;
 
69
                }
 
70
                if ( log.isDebugEnabled() ) {
 
71
                        log.debug( "Caching: " + key );
 
72
                }
 
73
 
 
74
                cache.put( key, value );
 
75
                return true;
 
76
 
 
77
        }
 
78
 
 
79
        /**
 
80
         * Do nothing.
 
81
         *
 
82
         * @return null, no lock
 
83
         */
 
84
        public SoftLock lock(Object key, Object version) throws CacheException {
 
85
                return null;
 
86
        }
 
87
 
 
88
        public void remove(Object key) throws CacheException {
 
89
                if ( log.isDebugEnabled() ) {
 
90
                        log.debug( "Removing: " + key );
 
91
                }
 
92
                cache.remove( key );
 
93
        }
 
94
 
 
95
        public void clear() throws CacheException {
 
96
                if ( log.isDebugEnabled() ) {
 
97
                        log.debug( "Clearing" );
 
98
                }
 
99
                cache.clear();
 
100
        }
 
101
 
 
102
        public void destroy() {
 
103
                try {
 
104
                        cache.destroy();
 
105
                }
 
106
                catch ( Exception e ) {
 
107
                        log.warn( "could not destroy cache", e );
 
108
                }
 
109
        }
 
110
 
 
111
        /**
 
112
         * Invalidate the item
 
113
         */
 
114
        public void evict(Object key) throws CacheException {
 
115
                if ( log.isDebugEnabled() ) {
 
116
                        log.debug( "Invalidating: " + key );
 
117
                }
 
118
 
 
119
                cache.remove( key );
 
120
        }
 
121
 
 
122
        /**
 
123
         * Invalidate the item
 
124
         */
 
125
        public boolean insert(Object key, Object value, Object currentVersion) {
 
126
                return false;
 
127
        }
 
128
 
 
129
        /**
 
130
         * Do nothing.
 
131
         */
 
132
        public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) {
 
133
                evict( key );
 
134
                return false;
 
135
        }
 
136
 
 
137
        /**
 
138
         * Invalidate the item (again, for safety).
 
139
         */
 
140
        public void release(Object key, SoftLock lock) throws CacheException {
 
141
                if ( log.isDebugEnabled() ) {
 
142
                        log.debug( "Invalidating (again): " + key );
 
143
                }
 
144
 
 
145
                cache.remove( key );
 
146
        }
 
147
 
 
148
        /**
 
149
         * Invalidate the item (again, for safety).
 
150
         */
 
151
        public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws CacheException {
 
152
                release( key, lock );
 
153
                return false;
 
154
        }
 
155
 
 
156
        /**
 
157
         * Do nothing.
 
158
         */
 
159
        public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
 
160
                return false;
 
161
        }
 
162
 
 
163
        public String getRegionName() {
 
164
                return cache.getRegionName();
 
165
        }
 
166
 
 
167
        public String toString() {
 
168
                return cache + "(nonstrict-read-write)";
 
169
        }
 
170
}