【问题标题】:Apache commons configuration cacheApache Commons 配置缓存
【发布时间】:2013-02-26 09:05:21
【问题描述】:

我正在研究如何在 apache-commons-configuration 框架中缓存我的属性。从不同的地方获取属性需要很长时间,在我的 config.xml 中定义。那么,Configuration 接口是否有缓存(例如按时间)实现?

【问题讨论】:

    标签: java apache-commons apache-commons-config


    【解决方案1】:
    • 您可以将 apache 对象保存在某个类的静态变量中,并在完成后设置为 null。使用静态 getter 读取它

    • 不确定 apache 配置 API,但我们使用静态 HashMap 并在其中存储属性。

    如果所有字符串:

    私有静态 Map 数据 = new 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。我看不出有什么问题?

    【讨论】:

      【解决方案2】:

      最后,我使用番石榴编写了自己的缓存:

      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);
          }
      }
      

      【讨论】:

        【解决方案3】:

        我扩展了 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;
        
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-29
          • 2018-07-25
          • 2012-01-28
          • 2023-03-10
          • 2011-08-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多