• 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/LocalCache.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 java.util.Map;
19
import java.util.concurrent.CompletableFuture;
20
import java.util.concurrent.ConcurrentMap;
21
import java.util.concurrent.Executor;
22
import java.util.function.BiFunction;
23
import java.util.function.Function;
24

25
import org.jspecify.annotations.Nullable;
26

27
import com.github.benmanes.caffeine.cache.stats.StatsCounter;
28

29
/**
30
 * An in-memory cache providing thread safety and atomicity guarantees. This interface provides an
31
 * extension to {@link ConcurrentMap} for use with skeletal implementations.
32
 *
33
 * @author ben.manes@gmail.com (Ben Manes)
34
 */
35
interface LocalCache<K, V> extends ConcurrentMap<K, V> {
36

37
  /** Returns whether this cache is asynchronous. */
38
  boolean isAsync();
39

40
  /** Returns whether this cache has statistics enabled. */
41
  boolean isRecordingStats();
42

43
  /** Returns the {@link StatsCounter} used by this cache. */
44
  StatsCounter statsCounter();
45

46
  /** Asynchronously sends a removal notification to the listener. */
47
  void notifyRemoval(@Nullable K key, @Nullable V value, RemovalCause cause);
48

49
  /** Returns the {@link Executor} used by this cache. */
50
  Executor executor();
51

52
  /** Returns the map of in-flight refresh operations. */
53
  ConcurrentMap<Object, CompletableFuture<?>> refreshes();
54

55
  /** Returns the {@link Expiry} used by this cache. */
56
  @Nullable Expiry<K, V> expiry();
57

58
  /** Returns the {@link Ticker} used by this cache for statistics. */
59
  Ticker statsTicker();
60

61
  /** See {@link Cache#estimatedSize()}. */
62
  long estimatedSize();
63

64
  /** Returns the reference key. */
65
  Object referenceKey(K key);
66

67
  /**
68
   * Returns whether an absent entry has expired or has been reference collected but has not yet
69
   * been removed from the cache.
70
   */
71
  boolean isPendingEviction(K key);
72

73
  /**
74
   * See {@link Cache#getIfPresent(K)}. This method differs by accepting a parameter of whether
75
   * to record the hit-and-miss statistics based on the success of this operation.
76
   */
77
  @Nullable
78
  V getIfPresent(K key, boolean recordStats);
79

80
  /**
81
   * See {@link Cache#getIfPresent(K)}. This method differs by not recording the access with
82
   * the statistics nor the eviction policy.
83
   */
84
  @Nullable
85
  V getIfPresentQuietly(Object key);
86

87
  /** See {@link Cache#getAllPresent}. */
88
  Map<K, V> getAllPresent(Iterable<? extends K> keys);
89

90
  /**
91
   * See {@link ConcurrentMap#replace(K, K, V)}. This method differs by optionally not discarding an
92
   * in-flight refresh for the entry if replaced.
93
   */
94
  boolean replace(K key, V oldValue, V newValue, boolean shouldDiscardRefresh);
95

96
  @Override
97
  default @Nullable V compute(K key,
98
      BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
99
    return compute(key, remappingFunction, expiry(),
×
100
        /* recordLoad= */ true, /* recordLoadFailure= */ true);
101
  }
102

103
  /**
104
   * See {@link ConcurrentMap#compute}. This method differs by accepting parameters indicating
105
   * whether to record load statistics based on the success of this operation.
106
   */
107
  @Nullable V compute(K key,
108
      BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction,
109
      @Nullable Expiry<? super K, ? super V> expiry, boolean recordLoad, boolean recordLoadFailure);
110

111
  @Override
112
  default @Nullable V computeIfAbsent(K key,
113
      Function<? super K, ? extends @Nullable V> mappingFunction) {
114
    return computeIfAbsent(key, mappingFunction, /* recordStats= */ true, /* recordLoad= */ true);
×
115
  }
116

117
  /**
118
   * See {@link ConcurrentMap#computeIfAbsent}. This method differs by accepting parameters
119
   * indicating how to record statistics.
120
   */
121
  @Nullable V computeIfAbsent(K key, Function<? super K, ? extends @Nullable V> mappingFunction,
122
      boolean recordStats, boolean recordLoad);
123

124
  /** See {@link Cache#invalidateAll(Iterable)}. */
125
  default void invalidateAll(Iterable<?> keys) {
126
    for (Object key : keys) {
×
127
      remove(key);
×
128
    }
×
129
  }
×
130

131
  /** See {@link Cache#cleanUp}. */
132
  void cleanUp();
133

134
  /** Notify the removal listener of a replacement if the value reference was changed. */
135
  @SuppressWarnings("FutureReturnValueIgnored")
136
  default void notifyOnReplace(K key, @Nullable V oldValue, V newValue) {
137
    if ((oldValue == null) || (oldValue == newValue)) {
×
138
      return;
×
139
    } else if (isAsync()) {
×
140
      var oldFuture = (CompletableFuture<?>) oldValue;
×
141
      var newFuture = (CompletableFuture<?>) newValue;
×
142
      newFuture.whenCompleteAsync((nv, e) -> {
×
143
        if (e == null) {
×
144
          oldFuture.thenAcceptAsync(ov -> {
×
145
            if (nv != ov) {
×
146
              notifyRemoval(key, oldValue, RemovalCause.REPLACED);
×
147
            }
148
          }, executor());
×
149
        } else {
150
          notifyRemoval(key, oldValue, RemovalCause.REPLACED);
×
151
        }
152
      }, executor());
×
153
    } else {
×
154
      notifyRemoval(key, oldValue, RemovalCause.REPLACED);
×
155
    }
156
  }
×
157

158
  /** Decorates the mapping function to record statistics if enabled, recording a miss if called. */
159
  default <T, R> Function<? super T, ? extends @Nullable R> statsAware(
160
      Function<? super T, ? extends @Nullable R> mappingFunction, boolean recordLoad) {
161
    if (!isRecordingStats()) {
×
162
      return mappingFunction;
×
163
    }
164
    return key -> {
×
165
      R value;
166
      statsCounter().recordMisses(1);
×
167
      long startTime = statsTicker().read();
×
168
      try {
169
        value = mappingFunction.apply(key);
×
170
      } catch (Throwable t) {
×
171
        statsCounter().recordLoadFailure(statsTicker().read() - startTime);
×
172
        throw t;
×
173
      }
×
174
      long loadTime = statsTicker().read() - startTime;
×
175
      if (recordLoad) {
×
176
        if (value == null) {
×
177
          statsCounter().recordLoadFailure(loadTime);
×
178
        } else {
179
          statsCounter().recordLoadSuccess(loadTime);
×
180
        }
181
      }
182
      return value;
×
183
    };
184
  }
185

186
  /** Decorates the remapping function to record statistics if enabled. */
187
  default <T, U, R> BiFunction<? super T, ? super U, ? extends @Nullable R> statsAware(
188
      BiFunction<? super T, ? super U, ? extends R> remappingFunction) {
189
    return statsAware(remappingFunction, /* recordLoad= */ true, /* recordLoadFailure= */ true);
×
190
  }
191

192
  /** Decorates the remapping function to record statistics if enabled. */
193
  default <T, U, R> BiFunction<? super T, ? super U, ? extends @Nullable R> statsAware(
194
      BiFunction<? super T, ? super U, ? extends @Nullable R> remappingFunction,
195
      boolean recordLoad, boolean recordLoadFailure) {
196
    if (!isRecordingStats()) {
×
197
      return remappingFunction;
×
198
    }
199
    return (t, u) -> {
×
200
      R result;
201
      long startTime = statsTicker().read();
×
202
      try {
203
        result = remappingFunction.apply(t, u);
×
204
      } catch (RuntimeException | Error e) {
×
205
        if (recordLoadFailure) {
×
206
          statsCounter().recordLoadFailure(statsTicker().read() - startTime);
×
207
        }
208
        throw e;
×
209
      }
×
210
      long loadTime = statsTicker().read() - startTime;
×
211
      if (recordLoad) {
×
212
        if (result == null) {
×
213
          statsCounter().recordLoadFailure(loadTime);
×
214
        } else {
215
          statsCounter().recordLoadSuccess(loadTime);
×
216
        }
217
      }
218
      return result;
×
219
    };
220
  }
221
}
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