1 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 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 38 public Object get(Object key, long txTimestamp) throws CacheException { 39 if ( log.isDebugEnabled() ) log.debug("Cache lookup: " + key); 40 41 Object 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 54 public boolean put( 55 Object key, 56 Object value, 57 long txTimestamp, 58 Object version, 59 Comparator 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 77 public SoftLock lock(Object key, Object version) throws CacheException { 78 return null; 79 } 80 81 public void remove(Object 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 e) { 96 log.warn("could not destroy cache", e); 97 } 98 } 99 100 103 public void evict(Object key) throws CacheException { 104 if ( log.isDebugEnabled() ) log.debug("Invalidating: " + key); 105 106 cache.remove(key); 107 } 108 109 112 public boolean update(Object key, Object value) throws CacheException { 113 evict(key); 114 return false; 115 } 116 117 120 public boolean insert(Object key, Object value) throws CacheException { 121 return false; 122 } 123 124 127 public void release(Object key, SoftLock lock) throws CacheException { 128 if ( log.isDebugEnabled() ) log.debug("Invalidating (again): " + key); 129 130 cache.remove(key); 131 } 132 133 136 public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws CacheException { 137 release(key, lock); 138 return false; 139 } 140 141 144 public boolean afterInsert(Object key, Object value, Object version) throws CacheException { 145 147 return false; 149 } 150 151 public String getRegionName() { 152 return cache.getRegionName(); 153 } 154 155 public String toString() { 156 return cache + "(nonstrict-read-write)"; 157 } 158 } 159 | Popular Tags |