• 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/LocalManualCache.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 java.util.Objects.requireNonNull;
20

21
import java.util.Collections;
22
import java.util.LinkedHashMap;
23
import java.util.LinkedHashSet;
24
import java.util.Map;
25
import java.util.Set;
26
import java.util.concurrent.ConcurrentMap;
27
import java.util.function.Function;
28

29
import org.jspecify.annotations.Nullable;
30

31
import com.github.benmanes.caffeine.cache.stats.CacheStats;
32
import com.google.errorprone.annotations.Var;
33

34
/**
35
 * This class provides a skeletal implementation of the {@link Cache} interface to minimize the
36
 * effort required to implement a {@link LocalCache}.
37
 *
38
 * @author ben.manes@gmail.com (Ben Manes)
39
 */
40
interface LocalManualCache<K, V> extends Cache<K, V> {
41

42
  /** Returns the backing {@link LocalCache} data store. */
43
  LocalCache<K, V> cache();
44

45
  @Override
46
  default long estimatedSize() {
47
    return cache().estimatedSize();
×
48
  }
49

50
  @Override
51
  default void cleanUp() {
52
    cache().cleanUp();
×
53
  }
×
54

55
  @Override
56
  default @Nullable V getIfPresent(K key) {
57
    return cache().getIfPresent(key, /* recordStats= */ true);
×
58
  }
59

60
  @Override
61
  @SuppressWarnings("NullAway")
62
  default @Nullable V get(K key, Function<? super K, ? extends V> mappingFunction) {
63
    return cache().computeIfAbsent(key, mappingFunction);
×
64
  }
65

66
  @Override
67
  default Map<K, V> getAllPresent(Iterable<? extends K> keys) {
68
    return cache().getAllPresent(keys);
×
69
  }
70

71
  @Override
72
  default Map<K, V> getAll(Iterable<? extends K> keys,
73
      Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction) {
74
    requireNonNull(mappingFunction);
×
75

76
    var found = cache().getAllPresent(keys);
×
77
    int initialCapacity = calculateHashMapCapacity(keys);
×
78
    var keysToLoad = new LinkedHashSet<K>(initialCapacity);
×
79
    var result = new LinkedHashMap<K, @Nullable V>(initialCapacity);
×
80
    for (K key : keys) {
×
81
      V value = found.get(key);
×
82
      if (value == null) {
×
83
        keysToLoad.add(key);
×
84
      }
85
      result.put(key, value);
×
86
    }
×
87
    if (keysToLoad.isEmpty()) {
×
88
      return found;
×
89
    }
90
    bulkLoad(keysToLoad, result, mappingFunction);
×
91

92
    @SuppressWarnings("NullableProblems")
93
    Map<K, V> unmodifiable = Collections.unmodifiableMap(result);
×
94
    return unmodifiable;
×
95
  }
96

97
  /**
98
   * Performs a non-blocking bulk load of the missing keys. Any missing entry that materializes
99
   * during the load are replaced when the loaded entries are inserted into the cache.
100
   */
101
  default void bulkLoad(Set<K> keysToLoad, Map<K, @Nullable V> result,
102
      Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction) {
103
    long startTime = cache().statsTicker().read();
×
104
    @Var boolean success = false;
×
105
    try {
106
      var loaded = mappingFunction.apply(Collections.unmodifiableSet(keysToLoad));
×
107
      loaded.forEach(cache()::put);
×
108
      for (K key : keysToLoad) {
×
109
        V value = loaded.get(key);
×
110
        if (value == null) {
×
111
          result.remove(key);
×
112
        } else {
113
          result.put(key, value);
×
114
        }
115
      }
×
116
      success = !loaded.isEmpty();
×
117
    } finally {
118
      long loadTime = cache().statsTicker().read() - startTime;
×
119
      if (success) {
×
120
        cache().statsCounter().recordLoadSuccess(loadTime);
×
121
      } else {
122
        cache().statsCounter().recordLoadFailure(loadTime);
×
123
      }
124
    }
125
  }
×
126

127
  @Override
128
  default void put(K key, V value) {
129
    cache().put(key, value);
×
130
  }
×
131

132
  @Override
133
  default void putAll(Map<? extends K, ? extends V> map) {
134
    cache().putAll(map);
×
135
  }
×
136

137
  @Override
138
  default void invalidate(K key) {
139
    cache().remove(key);
×
140
  }
×
141

142
  @Override
143
  default void invalidateAll(Iterable<? extends K> keys) {
144
    cache().invalidateAll(keys);
×
145
  }
×
146

147
  @Override
148
  default void invalidateAll() {
149
    cache().clear();
×
150
  }
×
151

152
  @Override
153
  default CacheStats stats() {
154
    return cache().statsCounter().snapshot();
×
155
  }
156

157
  @Override
158
  default ConcurrentMap<K, V> asMap() {
159
    return cache();
×
160
  }
161
}
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