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

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 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, @Nullable 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, @Nullable 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
    @SuppressWarnings("NullableProblems")
96
    Map<K, V> unmodifiable = Collections.unmodifiableMap(result);
×
97
    return unmodifiable;
×
98
  }
99

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

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

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

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

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

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

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

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

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

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

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

222
  /** Returns whether the supplied cache loader has bulk load functionality. */
223
  static boolean hasLoadAll(CacheLoader<?, ?> cacheLoader) {
224
    return hasMethodOverride(CacheLoader.class, cacheLoader, "loadAll", Set.class);
×
225
  }
226
}
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