【问题标题】:Implementing a Write-Back Cache in Java在 Java 中实现回写式缓存
【发布时间】:2011-05-07 01:14:56
【问题描述】:

我尝试实现回写式缓存。我正在尝试使用软引用,但在执行死后回写时遇到了麻烦,因为引用在添加到 gcQueue 之前已被清除,因此我无权访问引用对象。

解决方案?

【问题讨论】:

    标签: java caching soft-references


    【解决方案1】:

    你可以试试Guava'sMapmaker

    例子:

    final ConcurrentMap<Long, Integer> cache = new MapMaker()
            .softValues().expiration(20,TimeUnit.MINUTES)
            .makeComputingMap(new Function<Long, Integer>() {
                  @Override
                public Integer apply(Long arg0) {
                    return null;
                }
                });
    

    关于 MapMaker 的 SO 问题:

    备选方案:

    使用 Supplier 类的 memoizeWithExpiration,它也是 guava 库的一部分。

    【讨论】:

    • 我真的看不出它对回写有什么帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多