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

ben-manes / caffeine / #5155

03 Dec 2025 02:00AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5155

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/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 result = new LinkedHashMap<K, V>(initialCapacity);
×
79
    var keysToLoad = new LinkedHashSet<K>(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

91
    bulkLoad(keysToLoad, result, mappingFunction);
×
92
    return Collections.unmodifiableMap(result);
×
93
  }
94

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

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

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

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

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

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

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

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