• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5156

03 Dec 2025 03:21AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5156

push

github

ben-manes
add loading type to parameterized test dimensions to reduce task size

0 of 3834 branches covered (0.0%)

0 of 7848 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalLoadingCache.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.cache;
17

18
import static com.github.benmanes.caffeine.cache.Caffeine.calculateHashMapCapacity;
19
import static com.github.benmanes.caffeine.cache.Caffeine.hasMethodOverride;
20
import static com.github.benmanes.caffeine.cache.LocalAsyncCache.composeResult;
21
import static java.util.Objects.requireNonNull;
22

23
import java.lang.System.Logger;
24
import java.lang.System.Logger.Level;
25
import java.util.Collections;
26
import java.util.LinkedHashMap;
27
import java.util.Map;
28
import java.util.Set;
29
import java.util.concurrent.CancellationException;
30
import java.util.concurrent.CompletableFuture;
31
import java.util.concurrent.CompletionException;
32
import java.util.concurrent.TimeoutException;
33
import java.util.function.Function;
34

35
import org.jspecify.annotations.Nullable;
36

37
import com.google.errorprone.annotations.Var;
38

39
/**
40
 * This class provides a skeletal implementation of the {@link LoadingCache} interface to minimize
41
 * the effort required to implement a {@link LocalCache}.
42
 *
43
 * @author ben.manes@gmail.com (Ben Manes)
44
 */
45
interface LocalLoadingCache<K, V> extends LocalManualCache<K, V>, LoadingCache<K, V> {
46
  Logger logger = System.getLogger(LocalLoadingCache.class.getName());
×
47

48
  /** Returns the {@link AsyncCacheLoader} used by this cache. */
49
  AsyncCacheLoader<? super K, V> cacheLoader();
50

51
  /** Returns the {@link CacheLoader#load} as a mapping function. */
52
  Function<K, @Nullable V> mappingFunction();
53

54
  /** Returns the {@link CacheLoader#loadAll} as a mapping function, if implemented. */
55
  @Nullable Function<Set<? extends K>, Map<K, V>> bulkMappingFunction();
56

57
  @Override
58
  @SuppressWarnings("NullAway")
59
  default V get(K key) {
60
    return cache().computeIfAbsent(key, mappingFunction());
×
61
  }
62

63
  @Override
64
  default Map<K, V> getAll(Iterable<? extends K> keys) {
65
    Function<Set<? extends K>, Map<K, V>> mappingFunction = bulkMappingFunction();
×
66
    return (mappingFunction == null)
×
67
        ? loadSequentially(keys)
×
68
        : getAll(keys, mappingFunction);
×
69
  }
70

71
  /** Sequentially loads each missing entry. */
72
  default Map<K, V> loadSequentially(Iterable<? extends K> keys) {
73
    var result = new LinkedHashMap<K, V>(calculateHashMapCapacity(keys));
×
74
    for (K key : keys) {
×
75
      result.put(key, null);
×
76
    }
×
77

78
    @Var int count = 0;
×
79
    try {
80
      for (var iter = result.entrySet().iterator(); iter.hasNext();) {
×
81
        Map.Entry<K, V> entry = iter.next();
×
82
        count++;
×
83

84
        V value = get(entry.getKey());
×
85
        if (value == null) {
×
86
          iter.remove();
×
87
        } else {
88
          entry.setValue(value);
×
89
        }
90
      }
×
91
    } catch (Throwable t) {
×
92
      cache().statsCounter().recordMisses(result.size() - count);
×
93
      throw t;
×
94
    }
×
95
    return Collections.unmodifiableMap(result);
×
96
  }
97

98
  @Override
99
  @SuppressWarnings("FutureReturnValueIgnored")
100
  default CompletableFuture<V> refresh(K key) {
101
    requireNonNull(key);
×
102

103
    long[] startTime = new long[1];
×
104
    @SuppressWarnings({"unchecked", "Varifier"})
105
    @Nullable V[] oldValue = (V[]) new Object[1];
×
106
    @SuppressWarnings({"rawtypes", "unchecked"})
107
    CompletableFuture<? extends V>[] reloading = new CompletableFuture[1];
×
108
    Object keyReference = cache().referenceKey(key);
×
109

110
    var future = cache().refreshes().compute(keyReference, (k, existing) -> {
×
111
      if ((existing != null) && !Async.isReady(existing) && !cache().isPendingEviction(key)) {
×
112
        return existing;
×
113
      }
114

115
      try {
116
        startTime[0] = cache().statsTicker().read();
×
117
        oldValue[0] = cache().getIfPresentQuietly(key);
×
118
        var refreshFuture = (oldValue[0] == null)
×
119
            ? cacheLoader().asyncLoad(key, cache().executor())
×
120
            : cacheLoader().asyncReload(key, oldValue[0], cache().executor());
×
121
        reloading[0] = requireNonNull(refreshFuture, "Null future");
×
122
        return refreshFuture;
×
123
      } catch (RuntimeException e) {
×
124
        throw e;
×
125
      } catch (InterruptedException e) {
×
126
        Thread.currentThread().interrupt();
×
127
        throw new CompletionException(e);
×
128
      } catch (Exception e) {
×
129
        throw new CompletionException(e);
×
130
      }
131
    });
132

133
    if (reloading[0] != null) {
×
134
      reloading[0].whenComplete((newValue, error) -> {
×
135
        long loadTime = cache().statsTicker().read() - startTime[0];
×
136
        if (error != null) {
×
137
          if (!(error instanceof CancellationException) && !(error instanceof TimeoutException)) {
×
138
            logger.log(Level.WARNING, "Exception thrown during refresh", error);
×
139
          }
140
          cache().refreshes().remove(keyReference, reloading[0]);
×
141
          cache().statsCounter().recordLoadFailure(loadTime);
×
142
          return;
×
143
        }
144

145
        boolean[] discard = new boolean[1];
×
146
        var value = cache().compute(key, (k, currentValue) -> {
×
147
          boolean removed = cache().refreshes().remove(keyReference, reloading[0]);
×
148
          if (removed && (currentValue == oldValue[0])) {
×
149
            return (currentValue == null) && (newValue == null) ? null : newValue;
×
150
          }
151
          discard[0] = (currentValue != newValue);
×
152
          return currentValue;
×
153
        }, cache().expiry(), /* recordLoad= */ false, /* recordLoadFailure= */ true);
×
154

155
        if (discard[0] && (newValue != null)) {
×
156
          var cause = (value == null) ? RemovalCause.EXPLICIT : RemovalCause.REPLACED;
×
157
          cache().notifyRemoval(key, newValue, cause);
×
158
        }
159
        if (newValue == null) {
×
160
          cache().statsCounter().recordLoadFailure(loadTime);
×
161
        } else {
162
          cache().statsCounter().recordLoadSuccess(loadTime);
×
163
        }
164
      });
×
165
    }
166

167
    @SuppressWarnings("unchecked")
168
    var castedFuture = (CompletableFuture<V>) future;
×
169
    return castedFuture;
×
170
  }
171

172
  @Override
173
  default CompletableFuture<Map<K, V>> refreshAll(Iterable<? extends K> keys) {
174
    var result = new LinkedHashMap<K, CompletableFuture<V>>(calculateHashMapCapacity(keys));
×
175
    for (K key : keys) {
×
176
      result.computeIfAbsent(key, this::refresh);
×
177
    }
×
178
    return composeResult(result);
×
179
  }
180

181
  /** Returns a mapping function that adapts to {@link CacheLoader#load}. */
182
  static <K, V> Function<K, @Nullable V> newMappingFunction(CacheLoader<? super K, V> cacheLoader) {
183
    return key -> {
×
184
      try {
185
        return cacheLoader.load(key);
×
186
      } catch (RuntimeException e) {
×
187
        throw e;
×
188
      } catch (InterruptedException e) {
×
189
        Thread.currentThread().interrupt();
×
190
        throw new CompletionException(e);
×
191
      } catch (Exception e) {
×
192
        throw new CompletionException(e);
×
193
      }
194
    };
195
  }
196

197
  /** Returns a mapping function that adapts to {@link CacheLoader#loadAll}, if implemented. */
198
  static <K, V> @Nullable Function<Set<? extends K>, Map<K, V>> newBulkMappingFunction(
199
      CacheLoader<? super K, V> cacheLoader) {
200
    if (!hasLoadAll(cacheLoader)) {
×
201
      return null;
×
202
    }
203
    return keysToLoad -> {
×
204
      try {
205
        @SuppressWarnings("unchecked")
206
        var loaded = (Map<K, V>) cacheLoader.loadAll(keysToLoad);
×
207
        return loaded;
×
208
      } catch (RuntimeException e) {
×
209
        throw e;
×
210
      } catch (InterruptedException e) {
×
211
        Thread.currentThread().interrupt();
×
212
        throw new CompletionException(e);
×
213
      } catch (Exception e) {
×
214
        throw new CompletionException(e);
×
215
      }
216
    };
217
  }
218

219
  /** Returns whether the supplied cache loader has bulk load functionality. */
220
  static boolean hasLoadAll(CacheLoader<?, ?> cacheLoader) {
221
    return hasMethodOverride(CacheLoader.class, cacheLoader, "loadAll", Set.class);
×
222
  }
223
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc