1 16 package com.ibatis.sqlmap.engine.cache.oscache; 17 18 import com.ibatis.sqlmap.engine.cache.CacheController; 19 import com.ibatis.sqlmap.engine.cache.CacheModel; 20 import com.opensymphony.oscache.base.NeedsRefreshException; 21 import com.opensymphony.oscache.general.GeneralCacheAdministrator; 22 23 import java.util.Properties ; 24 25 28 public class OSCacheController implements CacheController { 29 30 private static final GeneralCacheAdministrator CACHE = new GeneralCacheAdministrator(); 31 32 public void flush(CacheModel cacheModel) { 33 CACHE.flushGroup(cacheModel.getId()); 34 } 35 36 public Object getObject(CacheModel cacheModel, Object key) { 37 String keyString = key.toString(); 38 try { 39 int refreshPeriod = (int) (cacheModel.getFlushIntervalSeconds()); 40 return CACHE.getFromCache(keyString, refreshPeriod); 41 } catch (NeedsRefreshException e) { 42 CACHE.cancelUpdate(keyString); 43 return null; 44 } 45 } 46 47 public Object removeObject(CacheModel cacheModel, Object key) { 48 Object result; 49 String keyString = key.toString(); 50 try { 51 int refreshPeriod = (int) (cacheModel.getFlushIntervalSeconds()); 52 Object value = CACHE.getFromCache(keyString, refreshPeriod); 53 if (value != null) { 54 CACHE.flushEntry(keyString); 55 } 56 result = value; 57 } catch (NeedsRefreshException e) { 58 try { 59 CACHE.flushEntry(keyString); 60 } finally { 61 CACHE.cancelUpdate(keyString); 62 result = null; 63 } 64 } 65 return result; 66 } 67 68 public void putObject(CacheModel cacheModel, Object key, Object object) { 69 String keyString = key.toString(); 70 CACHE.putInCache(keyString, object, new String []{cacheModel.getId()}); 71 } 72 73 public void configure(Properties props) { 74 } 75 76 } 77 | Popular Tags |