1
//$Id: NonstrictReadWriteCache.java 9278 2006-02-13 16:57:22Z steveebersole $
2
package org.hibernate.cache;
4
import java.util.Comparator;
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
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.
17
* @see ReadWriteCache for a much stricter algorithm
19
public class NonstrictReadWriteCache implements CacheConcurrencyStrategy {
23
private static final Log log = LogFactory.getLog( NonstrictReadWriteCache.class );
25
public NonstrictReadWriteCache() {
28
public void setCache(Cache cache) {
32
public Cache getCache() {
37
* Get the most recent version, if available.
39
public Object get(Object key, long txTimestamp) throws CacheException {
40
if ( log.isDebugEnabled() ) {
41
log.debug( "Cache lookup: " + key );
44
Object result = cache.get( key );
45
if ( result != null ) {
46
log.debug( "Cache hit" );
49
log.debug( "Cache miss" );
55
* Add an item to the cache.
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 );
70
if ( log.isDebugEnabled() ) {
71
log.debug( "Caching: " + key );
74
cache.put( key, value );
82
* @return null, no lock
84
public SoftLock lock(Object key, Object version) throws CacheException {
88
public void remove(Object key) throws CacheException {
89
if ( log.isDebugEnabled() ) {
90
log.debug( "Removing: " + key );
95
public void clear() throws CacheException {
96
if ( log.isDebugEnabled() ) {
97
log.debug( "Clearing" );
102
public void destroy() {
106
catch ( Exception e ) {
107
log.warn( "could not destroy cache", e );
112
* Invalidate the item
114
public void evict(Object key) throws CacheException {
115
if ( log.isDebugEnabled() ) {
116
log.debug( "Invalidating: " + key );
123
* Invalidate the item
125
public boolean insert(Object key, Object value, Object currentVersion) {
132
public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) {
138
* Invalidate the item (again, for safety).
140
public void release(Object key, SoftLock lock) throws CacheException {
141
if ( log.isDebugEnabled() ) {
142
log.debug( "Invalidating (again): " + key );
149
* Invalidate the item (again, for safety).
151
public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws CacheException {
152
release( key, lock );
159
public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
163
public String getRegionName() {
164
return cache.getRegionName();
167
public String toString() {
168
return cache + "(nonstrict-read-write)";