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

ben-manes / caffeine / #5573

03 Jul 2026 08:24PM UTC coverage: 98.975% (-1.0%) from 99.988%
#5573

push

github

ben-manes
Bound per-cycle expiration to amortize idle-recovery spikes

Cap expireAfterAccessEntries/expireAfterWriteEntries at
EXPIRATION_THRESHOLD evictions per maintenance cycle, then re-arm
via PROCESSING_TO_REQUIRED so the backlog drains across cycles.
Mirrors drainWriteBuffer's cap; keeps a large expired population
from stalling a writer-assist under evictionLock. The validation
subject pumps cleanUp until IDLE to drain the re-armed backlog.

3996 of 4072 branches covered (98.13%)

16 of 16 new or added lines in 1 file covered. (100.0%)

84 existing lines in 13 files now uncovered.

8205 of 8290 relevant lines covered (98.97%)

0.99 hits per line

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

93.88
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalCacheFactory.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.lang.invoke.MethodHandle;
19
import java.lang.invoke.MethodHandles;
20
import java.lang.invoke.MethodType;
21
import java.util.concurrent.ConcurrentHashMap;
22
import java.util.concurrent.ConcurrentMap;
23

24
import org.jspecify.annotations.Nullable;
25

26
import com.google.errorprone.annotations.Var;
27

28
/**
29
 * A factory for caches optimized for a particular configuration.
30
 *
31
 * @author ben.manes@gmail.com (Ben Manes)
32
 */
33
@FunctionalInterface
34
interface LocalCacheFactory {
35
  MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
1✔
36
  MethodType FACTORY = MethodType.methodType(
1✔
37
      void.class, Caffeine.class, AsyncCacheLoader.class, boolean.class);
38
  MethodType FACTORY_CALL = FACTORY.changeReturnType(BoundedLocalCache.class);
1✔
39
  ConcurrentMap<String, LocalCacheFactory> FACTORIES = new ConcurrentHashMap<>();
1✔
40
  String EXPIRES_AFTER_ACCESS_NANOS = "expiresAfterAccessNanos";
41
  String EXPIRES_AFTER_WRITE_NANOS = "expiresAfterWriteNanos";
42
  String REFRESH_AFTER_WRITE_NANOS = "refreshAfterWriteNanos";
43
  String WEIGHTED_SIZE = "weightedSize";
44
  String MAXIMUM = "maximum";
45

46
  /** Returns a cache optimized for this configuration. */
47
  <K, V> BoundedLocalCache<K, V> newInstance(Caffeine<K, V> builder,
48
      @Nullable AsyncCacheLoader<? super K, V> cacheLoader, boolean isAsync) throws Throwable;
49

50
  /** Returns a cache optimized for this configuration. */
51
  static <K, V> BoundedLocalCache<K, V> newBoundedLocalCache(Caffeine<K, V> builder,
52
      @Nullable AsyncCacheLoader<? super K, V> cacheLoader, boolean isAsync) {
53
    var className = getClassName(builder);
1✔
54
    var factory = loadFactory(className);
1✔
55
    try {
56
      return factory.newInstance(builder, cacheLoader, isAsync);
1✔
57
    } catch (RuntimeException | Error e) {
1✔
58
      throw e;
1✔
UNCOV
59
    } catch (Throwable t) {
×
UNCOV
60
      throw new IllegalStateException(className, t);
×
61
    }
62
  }
63

64
  static String getClassName(Caffeine<?, ?> builder) {
65
    var className = new StringBuilder();
1✔
66
    if (builder.isStrongKeys()) {
1✔
67
      className.append('S');
1✔
68
    } else {
69
      className.append('W');
1✔
70
    }
71
    if (builder.isStrongValues()) {
1✔
72
      className.append('S');
1✔
73
    } else {
74
      className.append('I');
1✔
75
    }
76
    if (builder.removalListener != null) {
1✔
77
      className.append('L');
1✔
78
    }
79
    if (builder.isRecordingStats()) {
1✔
80
      className.append('S');
1✔
81
    }
82
    if (builder.evicts()) {
1✔
83
      className.append('M');
1✔
84
      if (builder.isWeighted()) {
1✔
85
        className.append('W');
1✔
86
      } else {
87
        className.append('S');
1✔
88
      }
89
    }
90
    if (builder.expiresAfterAccess() || builder.expiresVariable()) {
1✔
91
      className.append('A');
1✔
92
    }
93
    if (builder.expiresAfterWrite()) {
1✔
94
      className.append('W');
1✔
95
    }
96
    if (builder.refreshAfterWrite()) {
1✔
97
      className.append('R');
1✔
98
    }
99
    return className.toString();
1✔
100
  }
101

102
  static LocalCacheFactory loadFactory(String className) {
103
    @Var var factory = FACTORIES.get(className);
1✔
104
    if (factory == null) {
1✔
105
      factory = FACTORIES.computeIfAbsent(className, LocalCacheFactory::newFactory);
1✔
106
    }
107
    return factory;
1✔
108
  }
109

110
  static LocalCacheFactory newFactory(String className) {
111
    try {
112
      var clazz = LOOKUP.findClass(LocalCacheFactory.class.getPackageName() + "." + className);
1✔
113
      try {
114
        // Fast path
115
        return (LocalCacheFactory) LOOKUP
1✔
116
            .findStaticVarHandle(clazz, "FACTORY", LocalCacheFactory.class).get();
1✔
117
      } catch (NoSuchFieldException e) {
1✔
118
        // Slow path when native hints are missing the field, but may have the constructor
UNCOV
119
        return new MethodHandleBasedFactory(clazz);
×
120
      }
121
    } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException t) {
1✔
122
      throw new IllegalStateException(className, t);
1✔
123
    }
124
  }
125

126
  final class MethodHandleBasedFactory implements LocalCacheFactory {
127
    final MethodHandle methodHandle;
128

129
    MethodHandleBasedFactory(Class<?> clazz) throws NoSuchMethodException, IllegalAccessException {
1✔
130
      this.methodHandle = LOOKUP.findConstructor(clazz, FACTORY).asType(FACTORY_CALL);
1✔
131
    }
1✔
132
    @SuppressWarnings({"ClassEscapesDefinedScope", "unchecked"})
133
    @Override public <K, V> BoundedLocalCache<K, V> newInstance(Caffeine<K, V> builder,
134
        @Nullable AsyncCacheLoader<? super K, V> cacheLoader, boolean async) throws Throwable {
135
      return (BoundedLocalCache<K, V>) methodHandle.invokeExact(builder, cacheLoader, async);
1✔
136
    }
137
  }
138
}
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