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

ben-manes / caffeine / #5157

03 Dec 2025 06:30AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5157

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

18
import static java.util.Objects.requireNonNull;
19
import static java.util.stream.Collectors.toUnmodifiableMap;
20

21
import java.lang.System.Logger;
22
import java.lang.System.Logger.Level;
23
import java.util.Map;
24
import java.util.Set;
25
import java.util.concurrent.TimeUnit;
26

27
import javax.cache.expiry.Duration;
28
import javax.cache.expiry.ExpiryPolicy;
29
import javax.cache.integration.CacheLoader;
30
import javax.cache.integration.CacheLoaderException;
31

32
import org.jspecify.annotations.Nullable;
33

34
import com.github.benmanes.caffeine.cache.Ticker;
35
import com.github.benmanes.caffeine.jcache.CacheProxy;
36
import com.github.benmanes.caffeine.jcache.Expirable;
37
import com.github.benmanes.caffeine.jcache.event.EventDispatcher;
38
import com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean;
39

40
/**
41
 * An adapter from a JCache cache loader to Caffeine's.
42
 *
43
 * @author ben.manes@gmail.com (Ben Manes)
44
 */
45
public final class JCacheLoaderAdapter<K, V>
46
    implements com.github.benmanes.caffeine.cache.CacheLoader<K, @Nullable Expirable<V>> {
47
  private static final Logger logger = System.getLogger(JCacheLoaderAdapter.class.getName());
×
48

49
  private final JCacheStatisticsMXBean statistics;
50
  private final EventDispatcher<K, V> dispatcher;
51
  private final CacheLoader<K, V> delegate;
52
  private final ExpiryPolicy expiry;
53
  private final Ticker ticker;
54

55
  private CacheProxy<K, V> cache;
56

57
  @SuppressWarnings("NullAway.Init")
58
  public JCacheLoaderAdapter(CacheLoader<K, V> delegate, EventDispatcher<K, V> dispatcher,
59
      ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics) {
×
60
    this.dispatcher = requireNonNull(dispatcher);
×
61
    this.statistics = requireNonNull(statistics);
×
62
    this.delegate = requireNonNull(delegate);
×
63
    this.expiry = requireNonNull(expiry);
×
64
    this.ticker = requireNonNull(ticker);
×
65
  }
×
66

67
  /**
68
   * Sets the cache instance that was created with this loader.
69
   *
70
   * @param cache the cache that uses this loader
71
   */
72
  public void setCache(CacheProxy<K, V> cache) {
73
    this.cache = requireNonNull(cache);
×
74
  }
×
75

76
  @Override
77
  public @Nullable Expirable<V> load(K key) {
78
    try {
79
      boolean statsEnabled = statistics.isEnabled();
×
80
      long start = statsEnabled ? ticker.read() : 0L;
×
81

82
      V value = delegate.load(key);
×
83
      if (value == null) {
×
84
        return null;
×
85
      }
86

87
      dispatcher.publishCreated(cache, key, value);
×
88

89
      if (statsEnabled) {
×
90
        // Subtracts the load time from the get time
91
        statistics.recordGetTime(start - ticker.read());
×
92
      }
93
      return new Expirable<>(value, expireTimeMillis());
×
94
    } catch (CacheLoaderException e) {
×
95
      throw e;
×
96
    } catch (RuntimeException e) {
×
97
      throw new CacheLoaderException(e);
×
98
    }
99
  }
100

101
  @Override
102
  public Map<K, Expirable<V>> loadAll(Set<? extends K> keys) {
103
    try {
104
      boolean statsEnabled = statistics.isEnabled();
×
105
      long start = statsEnabled ? ticker.read() : 0L;
×
106

107
      Map<K, Expirable<V>> result = delegate.loadAll(keys).entrySet().stream()
×
108
          .filter(entry -> (entry.getKey() != null) && (entry.getValue() != null))
×
109
          .collect(toUnmodifiableMap(Map.Entry::getKey,
×
110
              entry -> new Expirable<>(entry.getValue(), expireTimeMillis())));
×
111
      for (var entry : result.entrySet()) {
×
112
        dispatcher.publishCreated(cache, entry.getKey(), entry.getValue().get());
×
113
      }
×
114

115
      if (statsEnabled) {
×
116
        // Subtracts the load time from the get time
117
        statistics.recordGetTime(start - ticker.read());
×
118
      }
119
      return result;
×
120
    } catch (CacheLoaderException e) {
×
121
      throw e;
×
122
    } catch (RuntimeException e) {
×
123
      throw new CacheLoaderException(e);
×
124
    }
125
  }
126

127
  private long expireTimeMillis() {
128
    try {
129
      Duration duration = expiry.getExpiryForCreation();
×
130
      if (duration.isZero()) {
×
131
        return 0;
×
132
      } else if (duration.isEternal()) {
×
133
        return Long.MAX_VALUE;
×
134
      }
135
      long millis = TimeUnit.NANOSECONDS.toMillis(ticker.read());
×
136
      return duration.getAdjustedTime(millis);
×
137
    } catch (RuntimeException e) {
×
138
      logger.log(Level.WARNING, "Exception thrown by expiry policy", e);
×
139
      throw e;
×
140
    }
141
  }
142
}
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