1 package org.hibernate.cache; 3 4 import java.util.Map ; 5 6 import com.opensymphony.oscache.base.NeedsRefreshException; 7 import com.opensymphony.oscache.general.GeneralCacheAdministrator; 8 9 12 public class OSCache implements Cache { 13 14 17 private GeneralCacheAdministrator cache = new GeneralCacheAdministrator(); 18 19 private final int refreshPeriod; 20 private final String cron; 21 private final String regionName; 22 23 private String toString(Object key) { 24 return String.valueOf(key) + '.' + regionName; 25 } 26 27 public OSCache(int refreshPeriod, String cron, String region) { 28 this.refreshPeriod = refreshPeriod; 29 this.cron = cron; 30 this.regionName = region; 31 } 32 33 public void setCacheCapacity(int cacheCapacity) { 34 cache.setCacheCapacity(cacheCapacity); 35 } 36 37 public Object get(Object key) throws CacheException { 38 try { 39 return cache.getFromCache( toString(key), refreshPeriod, cron ); 40 } 41 catch (NeedsRefreshException e) { 42 cache.cancelUpdate( toString(key) ); 43 return null; 44 } 45 } 46 47 public Object read(Object key) throws CacheException { 48 return get(key); 49 } 50 51 public void update(Object key, Object value) throws CacheException { 52 put(key, value); 53 } 54 55 public void put(Object key, Object value) throws CacheException { 56 cache.putInCache( toString(key), value ); 57 } 58 59 public void remove(Object key) throws CacheException { 60 cache.flushEntry( toString(key) ); 61 } 62 63 public void clear() throws CacheException { 64 cache.flushAll(); 65 } 66 67 public void destroy() throws CacheException { 68 cache.destroy(); 69 } 70 71 public void lock(Object key) throws CacheException { 72 } 74 75 public void unlock(Object key) throws CacheException { 76 } 78 79 public long nextTimestamp() { 80 return Timestamper.next(); 81 } 82 83 public int getTimeout() { 84 return Timestamper.ONE_MS * 60000; } 86 87 public String getRegionName() { 88 return regionName; 89 } 90 91 public long getSizeInMemory() { 92 return -1; 93 } 94 95 public long getElementCountInMemory() { 96 return -1; 97 } 98 99 public long getElementCountOnDisk() { 100 return -1; 101 } 102 103 public Map toMap() { 104 throw new UnsupportedOperationException (); 105 } 106 107 public String toString() { 108 return "OSCache(" + regionName + ')'; 109 } 110 111 } 112 | Popular Tags |