java - Apache 公共(public)配置缓存

标签 java apache-commons apache-commons-config

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

最佳答案

最后,我使用 guava 编写了自己的缓存:

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

关于java - Apache 公共(public)配置缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334336/

相关文章:

java - HMS核心 map 套件, map 加载但不渲染

java - 检查 Java 版本是否大于 Java 中的某个迭代?

spring - 如何在仅给定文件的情况下创建 CommonsMultipartFile 对象

java - Apache Commons 配置 containsKey 在现有 key 上返回 false

Gradle 使用具有依赖项的插件抛出 ClassNotFoundException

java - DataOutputStream 不发送 -1

java - Itext 7 设置字体颜色

java - 使用 java 流查找第一个免费的 "index"

java - Struts2 jQuery struts-plugin.xml 无效

java - Jenkins Gradle 测试在 Commons Configuration 上失败