用Java缓存列表(或其他集合)的简单方法是什么 [英] What is a simple way to Cache a List (or other Collection) in Java

查看:203
本文介绍了用Java缓存列表(或其他集合)的简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以列表形式从数据库中拉出的集合,一旦被拉出就永远不会改变;系统中的每个用户都会看到相同的东西。我一直在尝试找出最简单的缓存方法。我知道我可以使用来自Guava的CacheBuilder,但是创建一个包含永远不变的项目的缓存地图似乎是过头的。

I have a Collection in the form of a List pulled out of the database that will never change once it is pulled; every user in the system will see the same thing. I have been trying to figure out the simplest way to cache it. I know I can use CacheBuilder from Guava, but it seems like overkill to create a cached map with 1 item in it that never changes.

推荐答案

在上面的@BenManes中,我使用了Suppliers.memoize:

From @BenManes above, I used Suppliers.memoize:

private Supplier<Collection<Person>> cache = Suppliers.memoizeWithExpiration(
        new Supplier<Collection<Employee>>() {
            public Collection<Employee> get() {
                return getAllEmployees();
            }
        }, 1, TimeUnit.DAYS);

public Collection<Employee> getAllEmployees() {
    return cache.get();
}

这篇关于用Java缓存列表(或其他集合)的简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆