用 Java 实现回写式缓存

发布于 2024-09-30 22:26:32 字数 95 浏览 0 评论 0原文

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

解决方案?

I trying to implement a write-back cache. I'm trying to use soft referenes, but I'm having troubles performing the post-mortum write-back because the reference is cleared before it's added to the gcQueue and thus I don't have access to the referent object.

Solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-10-07 22:26:32

您可以尝试 Guava Mapmaker

示例:

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 这也是番石榴库的一部分。

You can try Guava's Mapmaker.

Example:

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

SO Questions on MapMaker :

Alternative option :

Use Supplier class's memoizeWithExpiration which is also part of guava library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文