70

Google Guava has CacheBuilder that allows to create ConcurrentHash with expiring keys that allow to remove entries after the fixed tiemout. However I need to cache only one instance of certain type.

What is the best way to cache single object within fixed timeout using Google Guava?

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197

1 Answers1

132

I'd use Guava's Suppliers.memoizeWithExpiration(Supplier delegate, long duration, TimeUnit unit)

public class JdkVersionService {

    @Inject
    private JdkVersionWebService jdkVersionWebService;

    // No need to check too often. Once a year will be good :) 
    private final Supplier<JdkVersion> latestJdkVersionCache
            = Suppliers.memoizeWithExpiration(jdkVersionSupplier(), 365, TimeUnit.DAYS);


    public JdkVersion getLatestJdkVersion() {
        return latestJdkVersionCache.get();
    }

    private Supplier<JdkVersion> jdkVersionSupplier() {
        return new Supplier<JdkVersion>() {
            public JdkVersion get() {
                return jdkVersionWebService.checkLatestJdkVersion();
            }
        };
    }
}

Update with JDK 8

Today, I would write this code differently, using JDK 8 method references and constructor injection for cleaner code:

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.google.common.base.Suppliers;

@Service
public class JdkVersionService {

    private final Supplier<JdkVersion> latestJdkVersionCache;

    @Inject
    public JdkVersionService(JdkVersionWebService jdkVersionWebService) {
        this.latestJdkVersionCache = Suppliers.memoizeWithExpiration(
                jdkVersionWebService::checkLatestJdkVersion,
                365, TimeUnit.DAYS
        );
    }

    public JdkVersion getLatestJdkVersion() {
        return latestJdkVersionCache.get();
    }
}
Etienne Neveu
  • 12,604
  • 9
  • 36
  • 59
  • Do you know a solution for the case when the supplier is throwing a checked exception? – user2417480 Sep 23 '19 at 15:20
  • @user2417480 I would probably wrap that exception in a RuntimeException. Maybe unwrap it later if you really want to keep a checked exception, but it's usually an anti-pattern to carry around a checked exception across multiple layers... That's why e.g. spring-jdbc has a translation layer to convert checked exceptions into runtime DataAccessException. – Etienne Neveu Jan 22 '20 at 22:56