Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Memoize issue fix #1111

Merged
merged 5 commits into from
May 13, 2022
Merged

fix: Memoize issue fix #1111

merged 5 commits into from
May 13, 2022

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.

None yet

2 participants