2013-03-11 43 views
3

我在尋找如何在apache-commons-configuration框架中緩存我的屬性。花費很長時間才能從我的config.xml中定義的不同位置獲取屬性。那麼,有沒有一個緩存(例如,時間,例如)實現Configuration接口?Apache公用程序配置緩存

回答

1

最後,我所著我自己的緩存,使用番石榴:

public class Cfg { 
    private static Logger log = LoggerFactory.getLogger(Cfg.class); 
    private Configuration cfg; 
    private LoadingCache<String, Boolean> boolCache; 
    private LoadingCache<String, String> stringCache; 
    private LoadingCache<String, Float> floatCache; 
    private LoadingCache<String, Integer> integerCache; 
    private LoadingCache<String, List> listCache; 

    @PostConstruct 
    public void init() { 
     boolCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Boolean>() { 
      @Override 
      public Boolean load(String key) throws Exception { 
       return check(cfg.getBoolean(key), key); 
      } 
     }); 
     stringCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, String>() { 
      @Override 
      public String load(String key) throws Exception { 
       return check(cfg.getString(key), key); 
      } 
     }); 
     floatCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Float>() { 
      @Override 
      public Float load(String key) throws Exception { 
       return check(cfg.getFloat(key), key); 
      } 
     }); 
     integerCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Integer>() { 
      @Override 
      public Integer load(String key) throws Exception { 
       return check(cfg.getInt(key), key); 
      } 
     }); 
     listCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, List>() { 
      @Override 
      public List load(String key) throws Exception { 
       return check(cfg.getList(key), key); 
      } 
     }); 
    } 

    public boolean _bool(String key) { 
     try { 
      return boolCache.get(key); 
     } catch (ExecutionException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public float _float(String key) { 
     try { 
      return floatCache.get(key); 
     } catch (ExecutionException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public int _int(String key) { 
     try { 
      return integerCache.get(key); 
     } catch (ExecutionException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public String _string(String key) { 
     try { 
      return stringCache.get(key); 
     } catch (ExecutionException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public List<String> _list(String key) { 
     try { 
      //noinspection unchecked 
      return listCache.get(key); 
     } catch (ExecutionException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public void setCfg(Configuration cfg) { 
     this.cfg = cfg; 
    } 

    private <T> T check(T el, String key) { 
     if (el != null) { 
      return el; 
     } 
     throw new KeyNotFound(key); 
    } 
} 
1
  • 您可以將apache對象保存在某個類的靜態變量中,並在完成時將其設置爲null。有靜態的getter讀取它

  • 不確定有關apache配置API,但我們使用靜態HashMap和存儲屬性。

如果所有的字符串:

私有靜態地圖數據=新的HashMap();

可以公開爲屬性,所以你可以在任何地方使用

public class Props{ 

private static Map<String, String> data = new HashMap<String, String>(); 

public static void put(String name, String val){ 
    data.put(name,val); 
} 

public static String get(String name){ 
    return data.get(name) 
} 

public static void load(){//todo } 


public static void save(){//todo if needed if few change and need persistence} 

}

對於任何數據類型,除了原語

public class Props{ 

private static Map<String, Object> data = new HashMap<String, Object>(); 

public static void put(String name, Object val){ 
    data.put(name,val); 
} 

public static String get(String name){ 
    return data.get(name) 
} 

public static void load(){//todo } 


public static void save(){//todo if needed if few change and need persistence} 

}

如果你想要的對象是有時候可以使用WhirlyCache而不是HashMap。我沒有看到有什麼可以出錯?

1

我伸出DatabaseConfiguration因此它不打我的數據庫中的所有時間。 至於重新加載,我實例化我的配置,我需要它,並把它扔掉,當我完成它。

public class MyConfig extends DatabaseConfiguration { 

    private WeakHashMap<String,Object> cache = new WeakHashMap<String,Object>(); 

    public MyConfig(String datasourceString,String section) throws NamingException { 
     this((DataSource) new InitialContext().lookup(datasourceString),section); 
    } 

    protected MyConfig(DataSource datasource,String section) { 
     super(datasource, "COMMON_CONFIG","PROP_SECTION", "PROP_KEY", "PROP_VALUE",section); 
    } 

    @Override 
    public Object getProperty(String key){ 
     Object cachedValue = cache.get(key); 
     if (cachedValue != null){ 
      return cachedValue; 
     } 
     Object databaseValue = super.getProperty(key); 
     cache.put(key, databaseValue); 
     return databaseValue; 

    } 
}