Infinispan 6.4.0全局缓存过期时间设置方法咨询(含Spring配置场景)
Great question! You're right that Infinispan 6.4.0's HotRod client ConfigurationBuilder doesn't include a global expiration() method like newer versions (e.g., Infinispan 9). But there are two reliable approaches to achieve global cache-level expiration for your Spring Cache + Remote Cache Manager setup:
1. Server-Side Global Cache Template Configuration (Recommended)
Infinispan 6.4 server supports defining cache templates that all caches (including those accessed remotely) can inherit. This is the cleanest way to apply global expiration rules, as it centralizes the configuration on the server and requires no client-side changes.
Step 1: Update the Infinispan Server Configuration
Edit your server's configuration file (e.g., standalone.xml or domain.xml) to add a cache template with expiration settings, then make your caches inherit this template:
<cache-container name="default" default-cache="default"> <!-- Define a global template with expiration rules --> <local-cache-configuration name="global-expiring-template"> <!-- Set lifespan (time after creation until entry expires) in milliseconds --> <expiration lifespan="1000" /> <!-- Optional: Set max-idle (time after last access until entry expires) --> <!-- <expiration lifespan="1000" max-idle="500" /> --> </local-cache-configuration> <!-- Make the default cache inherit the template --> <local-cache name="default" configuration="global-expiring-template" /> <!-- Repeat for any other custom caches you use --> <local-cache name="user-cache" configuration="global-expiring-template" /> <local-cache name="product-cache" configuration="global-expiring-template" /> </cache-container>
- For distributed/replicated caches, replace
<local-cache-configuration>with<distributed-cache-configuration>or<replicated-cache-configuration>as needed. - All entries in caches using this template will automatically expire after the configured lifespan.
2. Client-Side Unified Expiration Setup
If you can't modify the server configuration, you can set default expiration rules for each cache when retrieving it via the HotRod client. This works well with Spring Cache by customizing the SpringRemoteCacheManager to apply global settings.
Step 2a: Customize SpringRemoteCacheManager
Override the cache retrieval logic to set default expiration for every cache your Spring application uses:
@Bean(name = "springRemoteCacheManager") public SpringRemoteCacheManager springRemoteCacheManager(RemoteCacheManager nativeCacheManager) { return new SpringRemoteCacheManager(nativeCacheManager) { @Override protected Cache getNativeCache(String cacheName) { RemoteCache<Object, Object> remoteCache = getNativeCacheManager().getCache(cacheName); // Set global default lifespan (1 second, matching your Infinispan 9 example) remoteCache.putDefaultLifespan(1, TimeUnit.SECONDS); // Optional: Set default max-idle if needed // remoteCache.putDefaultMaxIdle(30, TimeUnit.SECONDS); return remoteCache; } }; }
Step 2b: Alternative - Initialize Caches on Startup
Alternatively, you can pre-configure all required caches when initializing the RemoteCacheManager:
@Bean(name = "remoteCacheManager") public RemoteCacheManager remoteCacheManager(@Qualifier("cacheConfiguration") Configuration configuration) { RemoteCacheManager cacheManager = new RemoteCacheManager(configuration, true); // List all caches your application uses (can load from config if needed) List<String> cacheNames = Arrays.asList("default", "user-cache", "product-cache"); for (String cacheName : cacheNames) { RemoteCache<Object, Object> cache = cacheManager.getCache(cacheName); // Apply default expiration to each cache cache.putDefaultLifespan(1, TimeUnit.SECONDS); } return cacheManager; }
- The
putDefaultLifespan()method ensures all subsequentputoperations (including those triggered by Spring Cache annotations like@Cacheable) use this expiration time by default.
内容的提问来源于stack exchange,提问作者Priyanka

