在 Springboot 中同时使用 Caffeine 缓存和简单缓存时出现异常

getting exceptions when using both caffeine caching and simple caching in springboot

提问人:Harish Mahi 提问时间:10/11/2023 最后编辑:Harish Mahi 更新时间:10/11/2023 访问量:46

问:

我是 SpringBoot 的新手,并尝试根据要求实现咖啡因缓存和简单缓存。

如果我使用其中任何一个,它的编译和工作正常,但在同一类或不同类中一起使用时,我会遇到异常。

是否可以同时使用这两种缓存机制?

异常:

Oct 10, 2023 9:40:06 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [ROOT] appears to have started a thread named [cluster-ClusterId{value='6525775bd54ff65387d86e72', description='null'}-localhost:27017] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.net.SocketInputStream.socketRead0(Native Method)
 java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
 java.net.SocketInputStream.read(SocketInputStream.java:171)
 java.net.SocketInputStream.read(SocketInputStream.java:141)
 com.mongodb.internal.connection.SocketStream.read(SocketStream.java:109)
 com.mongodb.internal.connection.SocketStream.read(SocketStream.java:131)
 com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:647)
 com.mongodb.internal.connection.InternalStreamConnection.receiveMessageWithAdditionalTimeout(InternalStreamConnection.java:512)
 com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:355)
 com.mongodb.internal.connection.InternalStreamConnection.receive(InternalStreamConnection.java:315)
 com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:215)
 com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144)
 java.lang.Thread.run(Thread.java:750)
Oct 10, 2023 9:40:06 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [ROOT] appears to have started a thread named [cluster-rtt-ClusterId{value='6525775bd54ff65387d86e72', description='null'}-localhost:27017] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Thread.sleep(Native Method)
 com.mongodb.internal.connection.DefaultServerMonitor.waitForNext(DefaultServerMonitor.java:435)
 com.mongodb.internal.connection.DefaultServerMonitor.access$1300(DefaultServerMonitor.java:57)
 com.mongodb.internal.connection.DefaultServerMonitor$RoundTripTimeRunnable.run(DefaultServerMonitor.java:409)
 java.lang.Thread.run(Thread.java:750)

Java 类:

@Configuration
@EnableCaching
public class CacheConfig {


    @Bean
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCacheNames(Arrays.asList("task1"));
        cacheManager.setCaffeine(caffeineCacheBuilder());
        return cacheManager;
    }

    Caffeine<Object, Object> caffeineCacheBuilder() {
        return Caffeine.newBuilder()
                .expireAfterWrite(30, TimeUnit.DAYS) // Set TTL for 30 Days
                .maximumSize(5000); // Set maximum size to 5000 entries
    }

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("task2")));
        return cacheManager;
    }

}

Maven 库;

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>2.7.0</version>
</dependency>

注意:只有当我使用两种缓存机制并且使用其中任何一种时,我才会收到异常,它们会按预期工作

谢谢

java spring-boot spring-cache 咖啡因缓存

评论

0赞 M. Deinum 10/11/2023
这不是一个例外,而是一个警告/但是,为什么要使用 2 个缓存机制,为什么不坚持只使用咖啡因,因为这是一个适当的缓存,而地图则不是。
0赞 Harish Mahi 10/11/2023
@M.Deinum caffeine是新的实现,简单的缓存是现有的,已经在生产中。所以我不想破坏任何东西。

答:

0赞 Harish Mahi 10/11/2023 #1

进口

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

    @Bean
    public CacheManager cacheManager() {
        CompositeCacheManager cacheManager = new CompositeCacheManager();
        List<CacheManager> cacheManagers = Arrays.asList(caffeineCacheManager(), concurrentMapCacheManager());
        cacheManager.setCacheManagers(cacheManagers);
        return cacheManager;
    }
    @Bean
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        //add new cache here by separating with comma.
        cacheManager.setCacheNames(Arrays.asList("task1"));
        cacheManager.setCaffeine(caffeineCacheBuilder());
        return cacheManager;
    }
    Caffeine<Object, Object> caffeineCacheBuilder() {
        return Caffeine.newBuilder()
                .expireAfterWrite(30, TimeUnit.DAYS) // Set TTL for 30 Days
                .maximumSize(5000); // Set maximum size to 5000 entries
    }
    @Bean
    public CacheManager concurrentMapCacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("task2")));
        return cacheManager;
    }
}

评论

0赞 Community 10/16/2023
您的答案可以通过额外的支持信息得到改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。