2

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.

Kraagenskul
  • 424
  • 1
  • 7
  • 18

1 Answers1

4

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();
}
Kraagenskul
  • 424
  • 1
  • 7
  • 18