下面是用于创建 Ehcache 管理器 bean 的等效 java 配置。
应用程序配置.java
@Configuration
@EnableCaching
public class ApplicationConfig {
@Bean
public CacheManager ehCacheManager() {
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
CacheConfigurationBuilder<String, String> configuration =
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
String.class,
ResourcePoolsBuilder
.newResourcePoolsBuilder().offheap(1, MemoryUnit.MB))
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(20)));
javax.cache.configuration.Configuration<String, String> stringDoubleConfiguration =
Eh107Configuration.fromEhcacheCacheConfiguration(configuration);
cacheManager.createCache("users", stringDoubleConfiguration);
return cacheManager;
}
}
服务简介.java
@Service
public class ServiceImpl {
@Cacheable(cacheNames = {"users"})
public String getThis(String id) {
System.out.println("Method called............");
return "Value "+ id;
}
}
啪.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>