• 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
/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 @Nullable CacheProxy<K, V> cache;
56

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

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

75
  @Override
76
  @SuppressWarnings("ConstantValue")
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
      requireNonNull(cache);
×
88
      dispatcher.publishCreated(cache, key, value);
×
89

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

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

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

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

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