这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@johnmcclean
Copy link
Member

@johnmcclean johnmcclean commented May 11, 2022

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

The current implementation of Memoize.memoizeSupplierAsync creates caching function inside the supplier body which causes the caching function to get created on every Supplier.get() call.

Test Code:

    var supplier = Memoize.memoizeSupplierAsync(() -> {
      System.out.println(
          LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")) + ": refresh");
      return System.nanoTime();
    }, Executors.newSingleThreadScheduledExecutor(), 1_000);

    new Thread(() -> {
      for (int i = 0 ; i < 5; i++) {
        var d = supplier.get();
      }
    }).start();

Output:

2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:47: refresh
2022-05-11 02:10:48: refresh
2022-05-11 02:10:48: refresh
2022-05-11 02:10:48: refresh
2022-05-11 02:10:48: refresh
2022-05-11 02:10:48: refresh

Current Implementation.

  public static <R> Function0<R> memoizeSupplierAsync(Supplier<R> fn, ScheduledExecutorService ex, long updateRateInMillis) {
    return () -> {
      return memoizeFunctionAsync((a) -> {
        return fn.get();
      }, ex, updateRateInMillis).apply("k");
    };
  }

Right Implementation

  public static <R> Function0<R> memoizeSupplierAsync(Supplier<R> fn, ScheduledExecutorService ex, long updateRateInMillis) {
    Function1<T, R> memoizeFn = memoizeFunctionAsync((a) -> {
        return fn.get();
      }, ex, updateRateInMillis);

    return () -> {
      return memoizeFn.apply("k");
    };
  }

Also upgrades to latest Jackson Databind.

@johnmcclean johnmcclean merged commit c987af4 into master May 13, 2022
@johnmcclean johnmcclean deleted the memoizeFix branch May 13, 2022 21:38
@johnmcclean johnmcclean added this to the 10.4.1 milestone May 15, 2022
johnmcclean added a commit that referenced this pull request May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants