• 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
/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();
×
36
  MethodType FACTORY = MethodType.methodType(
×
37
      void.class, Caffeine.class, AsyncCacheLoader.class, boolean.class);
38
  MethodType FACTORY_CALL = FACTORY.changeReturnType(BoundedLocalCache.class);
×
39
  ConcurrentMap<String, LocalCacheFactory> FACTORIES = new ConcurrentHashMap<>();
×
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);
×
54
    var factory = loadFactory(className);
×
55
    try {
56
      return factory.newInstance(builder, cacheLoader, isAsync);
×
57
    } catch (RuntimeException | Error e) {
×
58
      throw e;
×
59
    } catch (Throwable t) {
×
60
      throw new IllegalStateException(className, t);
×
61
    }
62
  }
63

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

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

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

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

129
    MethodHandleBasedFactory(Class<?> clazz) throws NoSuchMethodException, IllegalAccessException {
×
130
      this.methodHandle = LOOKUP.findConstructor(clazz, FACTORY).asType(FACTORY_CALL);
×
131
    }
×
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);
×
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