Guava LoadingCache Example: Demoed With Java8 Duration and Instant

Guava LoadingCache is a very convenient utility for caching key based value for performance improvement. Whenever you have an expensive object or calculation can be identified with a key, you always should consider to cache it in one way or another, especially on the system where the memory is relatively abundant.

Here below is a very simple program to demonstrate the LoadingCache. To run it, you need to put Guava jar on the classpath.

package test;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;


public class Test {

  // Create a cache
  private static final LoadingCache<String, ExpensiveObject> cache = CacheBuilder
      .newBuilder().maximumSize(5).expireAfterAccess(5, TimeUnit.MINUTES)
      .build(new CacheLoader<String, ExpensiveObject>() {

        public ExpensiveObject load(String id) throws Exception {
          return ExpensiveObject.getInstance(id);

  public static void main(String[] args) throws IOException, ExecutionException {
    Instant t0 =;
    Instant t1 =;
    Instant t2 =;
    Instant t3 =;
    Instant t4 =;
    Instant t5 =;
    System.out.println("First time for a used " + Duration.between(t0, t1).toMillis());
    System.out.println("First time for b used " + Duration.between(t1, t2).toMillis());
    System.out.println("Second time for a used " + Duration.between(t2, t3).toMillis());
    System.out.println("Second time for b used " + Duration.between(t3, t4).toMillis());
    System.out.println("First time for c used " + Duration.between(t4, t5).toMillis());

  public static class ExpensiveObject{
    private final String id;

    private ExpensiveObject(String id) throws InterruptedException {
      //Simulate an expensive operation, such as database access or network downloading, etc

    public static ExpensiveObject getInstance(String id) throws InterruptedException {
      return new ExpensiveObject(id);

The output of the execution should be

First time for a used 5008
First time for b used 5001
Second time for a used 0
Second time for b used 0
First time for c used 5000

The LoadingCache is very easy to configure. The maximum number of cached entries is specified by maximumSize(int), and the cached entry expiration duration is specified by expireAfterAccess(int, TimeUnit). When configuring, you should take your application’s nature into consideration. For example how much heap you can afford for caching and how frequent the cached objects mutate.

This entry was posted in Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s