i am new to springboot and trying to implement both caffeine caching and simple caching based on a requirement.
if i use any one them its compiling and working fine but using together in same class or different class i am getting exceptions.
is it okay to use both caching mechanisms?
Exceptions:
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 Class:
@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 library;
<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>
Note: i am getting exceptions only if i use both cache mechanisms and if i use any one of them they are working as expected
Thanks
//imports
@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;
}
}