This is the 9th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021.

Create a LoadingCache in KV format using The CacheBuilder. What if all you need to cache is a value?

For this scenario, we introduce a caching approach based on Supplier

1. Supplier uses posture

The Supplier in Guava is no different from the Supplier in the JDK in terms of interface definition, providing only a get() method

@FunctionalInterface
@GwtCompatible
public interface Supplier<T> extends java.util.function.Supplier<T> {
    @CanIgnoreReturnValue
    T get(a);
}
Copy the code

The important thing to focus on is the posture that Suppliers create, with Suppliers

Here are a few common creation poses:

  • memoizeDelegate is the concrete delegate class that gets a value. Note that the concrete implementation of delegate is called only the first time; This approach is equivalent to persistent caching
  • memoizeWithExpirationThe delegate return value is cached for a while; After the cache time has elapsed, the delegate is called again to get the return value
  • ofInstance: Directly pass the parameter
public static <T> Supplier<T> memoize(Supplier<T> delegate)

public static <T> Supplier<T> memoizeWithExpiration(Supplier<T> delegate, long duration, TimeUnit unit)

public static <T> Supplier<T> ofInstance(@Nullable T instance)
Copy the code

Based on the method described above, if we want to implement a 10s cache, we can choose memoizeWithExpiration to implement it

AtomicInteger atomicInteger = new AtomicInteger(1);
Supplier<Integer> cache = Suppliers.memoizeWithExpiration(this::ret, 10, TimeUnit.SECONDS);

private int ret(a) {
    System.out.println("------- update value --------");
    return atomicInteger.getAndAdd(2); }Copy the code

If the cache fails, ret() will be called again to refresh the cache

The test case is easier

@Test
public void testSupplier(a) throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        System.out.print(cache.get() + "|");
    }
    System.out.println();
    Thread.sleep(10000);
    System.out.println(cache.get());
}
Copy the code

The output is as follows

-- -- -- -- -- -- -- to update the value -- -- -- -- -- -- -- - | 1 | | 1 | | 1 | | 1 | | 1 | -- -- -- -- -- -- -- to update the value -- -- -- -- -- -- -- -- 3Copy the code

2. Refresh the cache

When using Supplier for caching, one thing to note is that there are no cache invalidation methods to call; To invalidate a LoadingCache, Supplier automatically invalidates the specified cache by calling invalidate.

  • Reassign directly

For example, when we want to refresh, we can simply override the existing supplier

public void refresh(a) {
    cache = Suppliers.memoizeWithExpiration(this::ret, 10, TimeUnit.SECONDS);
}
Copy the code

A gray contact information

All letter is better than no book, the above content, purely one’s words, due to the limited personal ability, it is hard to avoid omissions and mistakes, such as finding bugs or better suggestions, welcome criticism and correction, not grudging gratitude

  • Personal site: blog.hhui.top
  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840
  • Wechat official account: One Grey Blog