简体   繁体   中英

Caching using Supplier Java 8

This method, as I know, memorizes (caches) the value of supplier that passed as the parameter. As I understand It behaves like singleton pattern. Can anyone explain how it works?

  public static <T> Supplier<T> memoize(final Supplier<? extends T> valueSupplier)
  {
    final List<T> memoryList= new ArrayList<>();
    return () -> {
      if (memoryList.isEmpty()) {
        memoryList.add(valueSupplier.get());
      }
      return memoryList.get(0);
    };
  }

Usage like this:

Supplier<SomeClass> cachedValue = memoize(() -> someClassObject.getSomeValueToBeCached());
cachedValue.get().doMethod();

Ok, so let's rewrite the code in small steps towards more old-style, verbose Java. Maybe that makes it simpler to understand.

First step: get rid of the lambda:

public static <T> Supplier<T> memoize(final Supplier<? extends T> valueSupplier)
{
    final List<T> memoryList= new ArrayList<>();
    return new Supplier<T>() {
        @Override
        public T get() {
            if (memoryList.isEmpty()) {
                memoryList.add(valueSupplier.get());
            }
            return memoryList.get(0);
        }
    };
}

Next step: extract the anonymous inner class into a standalone class. While the anonymous class had access to the local variables of its containing method ( memoryList ), a "normal" class has not, so we're moving the list into the caching supplier.

class CachingSupplier<T> implements Supplier<T> {

    final List<T> memoryList= new ArrayList<>();
    private Supplier<T> originalSupplier;

    public CachingSupplier(Supplier<T> originalSupplier) {
        this.originalSupplier = originalSupplier;
    }

    @Override
    public T get() {
        if (memoryList.isEmpty()) {
            memoryList.add(originalSupplier.get());
        }
        return memoryList.get(0);
    }
}

public static <T> Supplier<T> memoize(final Supplier<? extends T> valueSupplier) {
  return new CachingSupplier<>(valueSupplier);
}

Finally, let's replace the ArrayList by a simple reference.

class CachingSupplier<T> implements Supplier<T> {

    private T cachedValue;
    private Supplier<T> originalSupplier;

    public CachingSupplier(Supplier<T> originalSupplier) {
        this.originalSupplier = originalSupplier;
    }

    @Override
    public T get() {
        if (cachedValue == null) {
            cachedValue = originalSupplier.get();
        }
        return cachedValue;
    }
}

public static <T> Supplier<T> memoize(final Supplier<? extends T> valueSupplier) {
  return new CachingSupplier<>(valueSupplier);
}

Maybe that's easier to understand. If you are still unclear about something, just ask in a comment and I'll try to explain it.

How about this?

public static <T> Supplier<T> memoize(final Supplier<? extends T> factory) {
    final List<T> cache = new ArrayList<>();
    return () -> {
               // v--- check the value is cached?
        if (cache.isEmpty()) {
                           // v--- return the value created by factory
            cache.add(factory.get());
               // ^--- adding the value into the cache
        }
        return cache.get(0); 
                  // ^--- return the cached value
    };
}

Usage

Supplier<String> factory = ()-> new String("foo"); 

assert factory.get() == factory.get(); // return false;
assert memoize(factory).get() == memoize(factory).get(); //return false;

                 // v--- storing the memoized factory for using as further
Supplier<String> memoized = memoize(original);
assert memoized.get() == memoized.get(); // return true.
                    // ^--- they are the same.  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM