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

ben-manes / caffeine / #5651

18 Jul 2026 03:26AM UTC coverage: 66.303% (-33.7%) from 100.0%
#5651

push

github

ben-manes
dependency updates

2809 of 4164 branches covered (67.46%)

5584 of 8422 relevant lines covered (66.3%)

0.66 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
    try {
64
      return getOrLoad(key);
×
65
    } catch (NullPointerException | IllegalStateException | ClassCastException | CacheException e) {
×
66
      throw e;
×
67
    } catch (RuntimeException e) {
×
68
      throw new CacheException(e);
×
69
    } finally {
70
      dispatcher.awaitSynchronous();
×
71
    }
72
  }
73

74
  /** Retrieves the value from the cache, loading it if necessary. */
75
  private @Nullable V getOrLoad(K key) {
76
    boolean statsEnabled = statistics.isEnabled();
×
77
    long start = statsEnabled ? ticker.read() : 0L;
×
78

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

97
    if (expirable == null) {
×
98
      expirable = cache.get(copyOf(key));
×
99
      statistics.recordMisses(1L);
×
100
    } else {
101
      setAccessExpireTime(key, expirable, millis);
×
102
      statistics.recordHits(1L);
×
103
    }
104

105
    @Var V value = null;
×
106
    if (expirable != null) {
×
107
      value = copyOf(expirable.get());
×
108
    }
109
    if (statsEnabled) {
×
110
      statistics.recordGetTime(ticker.read() - start);
×
111
    }
112
    return value;
×
113
  }
114

115
  @Override
116
  public Map<K, V> getAll(Set<? extends K> keys) {
117
    requireNotClosed();
×
118
    boolean statsEnabled = statistics.isEnabled();
×
119
    long start = statsEnabled ? ticker.read() : 0L;
×
120
    try {
121
      Map<K, Expirable<V>> entries = getAndFilterExpiredEntries(keys);
×
122

123
      if (entries.size() != keys.size()) {
×
124
        List<K> keysToLoad = keys.stream()
×
125
            .filter(key -> !entries.containsKey(key))
×
126
            .map(this::copyOf)
×
127
            .collect(toUnmodifiableList());
×
128
        entries.putAll(cache.getAll(keysToLoad));
×
129
      }
130

131
      Map<K, V> result = copyMap(entries);
×
132
      if (statsEnabled) {
×
133
        statistics.recordGetTime(ticker.read() - start);
×
134
      }
135
      return result;
×
136
    } catch (NullPointerException | IllegalStateException | ClassCastException | CacheException e) {
×
137
      throw e;
×
138
    } catch (RuntimeException e) {
×
139
      throw new CacheException(e);
×
140
    } finally {
141
      dispatcher.awaitSynchronous();
×
142
    }
143
  }
144
}
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