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

ben-manes / caffeine / #5707

02 Aug 2026 12:45AM UTC coverage: 0.106% (-99.9%) from 100.0%
#5707

push

github

ben-manes
fix wikibench trace reader after overly strict audit fixes

0 of 4235 branches covered (0.0%)

9 of 8528 relevant lines covered (0.11%)

0.0 hits per line

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

0.0
/jcache/src/main/java/com/github/benmanes/caffeine/jcache/LoadingCacheProxy.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.jcache;
17

18
import static java.util.stream.Collectors.toUnmodifiableList;
19

20
import java.util.List;
21
import java.util.Map;
22
import java.util.Optional;
23
import java.util.Set;
24
import java.util.concurrent.Executor;
25

26
import javax.cache.Cache;
27
import javax.cache.CacheException;
28
import javax.cache.expiry.ExpiryPolicy;
29
import javax.cache.integration.CacheLoader;
30

31
import org.jspecify.annotations.Nullable;
32

33
import com.github.benmanes.caffeine.cache.LoadingCache;
34
import com.github.benmanes.caffeine.cache.Ticker;
35
import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration;
36
import com.github.benmanes.caffeine.jcache.copy.Copier;
37
import com.github.benmanes.caffeine.jcache.event.EventDispatcher;
38
import com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean;
39
import com.google.errorprone.annotations.Var;
40

41
/**
42
 * An implementation of JSR-107 {@link Cache} backed by a Caffeine loading cache.
43
 *
44
 * @author ben.manes@gmail.com (Ben Manes)
45
 */
46
@SuppressWarnings("OvershadowingSubclassFields")
47
public final class LoadingCacheProxy<K, V> extends CacheProxy<K, V> {
48
  private final LoadingCache<K, @Nullable Expirable<V>> cache;
49

50
  @SuppressWarnings({"PMD.ExcessiveParameterList", "TooManyParameters"})
51
  public LoadingCacheProxy(String name, Executor executor, CacheManagerImpl cacheManager,
52
      CaffeineConfiguration<K, V> configuration, LoadingCache<K, @Nullable Expirable<V>> cache,
53
      EventDispatcher<K, V> dispatcher, CacheLoader<K, V> cacheLoader, ExpiryPolicy expiry,
54
      Ticker ticker, JCacheStatisticsMXBean statistics, Copier copier) {
55
    super(name, executor, cacheManager, configuration, cache, dispatcher,
×
56
        Optional.of(cacheLoader), expiry, ticker, statistics, copier);
×
57
    this.cache = cache;
×
58
  }
×
59

60
  @Override
61
  public @Nullable V get(K key) {
62
    requireNotClosed();
×
63
    @Nullable V value;
64
    try {
65
      value = getOrLoad(key);
×
66
    } catch (NullPointerException | IllegalStateException | ClassCastException | CacheException e) {
×
67
      awaitAndSuppressFailure(e);
×
68
      throw e;
×
69
    } catch (RuntimeException e) {
×
70
      var error = new CacheException(e);
×
71
      awaitAndSuppressFailure(error);
×
72
      throw error;
×
73
    }
×
74
    var listenerFailure = awaitSynchronousFailure();
×
75
    if (listenerFailure != null) {
×
76
      throw listenerFailure;
×
77
    }
78
    return value;
×
79
  }
80

81
  /** Retrieves the value from the cache, loading it if necessary. */
82
  private @Nullable V getOrLoad(K key) {
83
    boolean statsEnabled = statistics.isEnabled();
×
84
    long start = statsEnabled ? ticker.read() : 0L;
×
85

86
    @Var long millis = 0L;
×
87
    @Var Expirable<V> expirable = cache.getIfPresent(key);
×
88
    if ((expirable != null) && !expirable.isEternal()) {
×
89
      millis = nanosToMillis((start == 0L) ? ticker.read() : start);
×
90
      if (expirable.hasExpired(millis)) {
×
91
        var expired = expirable;
×
92
        cache.asMap().computeIfPresent(key, (k, e) -> {
×
93
          if (e == expired) {
×
94
            dispatcher.publishExpired(this, key, expired.get());
×
95
            statistics.recordEvictions(1L);
×
96
            return null;
×
97
          }
98
          return e;
×
99
        });
100
        expirable = null;
×
101
      }
102
    }
103

104
    if (expirable == null) {
×
105
      statistics.recordMisses(1L);
×
106
      expirable = cache.get(copyOf(key));
×
107
    } else {
108
      var duration = getAccessExpireTime();
×
109
      setVariableExpiration(key, duration);
×
110
      setAccessExpireTime(expirable, duration, millis);
×
111
      statistics.recordHits(1L);
×
112
    }
113

114
    @Var V value = null;
×
115
    if (expirable != null) {
×
116
      value = copyOf(expirable.get());
×
117
    }
118
    if (statsEnabled) {
×
119
      statistics.recordGetTime(ticker.read() - start);
×
120
    }
121
    return value;
×
122
  }
123

124
  @Override
125
  public Map<K, V> getAll(Set<? extends K> keys) {
126
    requireNotClosed();
×
127
    boolean statsEnabled = statistics.isEnabled();
×
128
    long start = statsEnabled ? ticker.read() : 0L;
×
129
    Map<K, V> result;
130
    try {
131
      Map<K, Expirable<V>> entries = getAndFilterExpiredEntries(keys);
×
132

133
      if (entries.size() != keys.size()) {
×
134
        List<K> keysToLoad = keys.stream()
×
135
            .filter(key -> !entries.containsKey(key))
×
136
            .map(this::copyOf)
×
137
            .collect(toUnmodifiableList());
×
138
        entries.putAll(cache.getAll(keysToLoad));
×
139
      }
140

141
      result = copyMap(entries);
×
142
      if (statsEnabled) {
×
143
        statistics.recordGetTime(ticker.read() - start);
×
144
      }
145
    } catch (NullPointerException | IllegalStateException | ClassCastException | CacheException e) {
×
146
      awaitAndSuppressFailure(e);
×
147
      throw e;
×
148
    } catch (RuntimeException e) {
×
149
      var error = new CacheException(e);
×
150
      awaitAndSuppressFailure(error);
×
151
      throw error;
×
152
    }
×
153
    var listenerFailure = awaitSynchronousFailure();
×
154
    if (listenerFailure != null) {
×
155
      throw listenerFailure;
×
156
    }
157
    return result;
×
158
  }
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc