//$Id: OSCache.java,v 1.6 2005/01/13 19:53:01 oneovthafew Exp $ package org.hibernate.cache; import java.util.Map; import java.util.Properties; import org.hibernate.util.PropertiesHelper; import com.opensymphony.oscache.base.CacheEntry; import com.opensymphony.oscache.base.Config; import com.opensymphony.oscache.base.NeedsRefreshException; import com.opensymphony.oscache.general.GeneralCacheAdministrator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author Mathias Bogaert * Adapter for the OSCache implementation */ public class OSCache implements Cache { private static final Log log = LogFactory.getLog(OSCache.class); /** * The OSCache cache capacity property suffix. */ public static final String OSCACHE_CAPACITY = "cache.capacity"; private static final Properties OSCACHE_PROPERTIES = new Config().getProperties(); /** * The OSCache 2.0 cache administrator. */ private static GeneralCacheAdministrator cache = new GeneralCacheAdministrator(); private static Integer capacity = PropertiesHelper.getInteger(OSCACHE_CAPACITY, OSCACHE_PROPERTIES); static { if (capacity != null) cache.setCacheCapacity(capacity.intValue()); } private final int refreshPeriod; private final String cron; private final String regionName; private final String[] regionGroups; private String toString(Object key) { return String.valueOf(key); //used StringHelper.DOT instead of "." in hib2.1: has this constant moved elsewhere? //return String.valueOf(key) +"." + regionName; } public OSCache(int refreshPeriod, String cron, String region) { this.refreshPeriod = refreshPeriod; this.cron = cron; this.regionName = region; this.regionGroups = new String[] {region}; log.error("!!!!!New OSCache for "+region); } public void setCacheCapacity(int cacheCapacity) { cache.setCacheCapacity(cacheCapacity); } public Object get(Object key) throws CacheException { try { return cache.getFromCache( toString(key), refreshPeriod, cron ); } catch (NeedsRefreshException e) { cache.cancelUpdate( toString(key) ); return null; } } public void put(Object key, Object value) throws CacheException { if( get(key) != null ) log.error("!!!!!!!SHOULD SEND FLUSH NOTIFICATION (put "+toString(key)+" for "+regionName+") !!!!!!!!!!"); cache.putInCache( toString(key), value, regionGroups ); } public void remove(Object key) throws CacheException { cache.flushEntry( toString(key) ); log.error("!!!!!!!SHOULD SEND FLUSH NOTIFICATION (remove "+toString(key)+" for "+regionName+") !!!!!!!!!!"); } public void clear() throws CacheException { cache.flushGroup(regionName); log.error("!!!!!!!SHOULD SEND FLUSH NOTIFICATION (clear for "+regionName+") !!!!!!!!!!"); } public void destroy() throws CacheException { cache.destroy(); } public void lock(Object key) throws CacheException { // local cache, so we use synchronization } public void unlock(Object key) throws CacheException { // local cache, so we use synchronization } public long nextTimestamp() { return Timestamper.next(); } public int getTimeout() { return CacheEntry.INDEFINITE_EXPIRY; } public String getRegionName() { return regionName; } public long getSizeInMemory() { return -1; } public long getElementCountInMemory() { return -1; } public long getElementCountOnDisk() { return -1; } public Map toMap() { throw new UnsupportedOperationException(); } public String toString() { return "OSCache(" + regionName + ')'; } }