怎么开启ehcache3的统计功能 用配置类打开
时间: 2023-02-06 19:04:13 浏览: 109
要使用配置类打开 Ehcache 3 的统计功能,您需要在配置类中添加以下内容:
```
@Configuration
@EnableCaching
public class MyCacheConfig {
@Bean
public CacheManager cacheManager() {
CacheConfiguration<Long, String> cacheConfiguration =
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10))
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
.withDisabled(false)
.withStatistics(true)
.build();
Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
caches.put("myCache", cacheConfiguration);
return CacheManagerBuilder.newCacheManagerBuilder()
.withCache("myCache", cacheConfiguration)
.build(true);
}
}
```
在这里,我们使用了 `CacheConfigurationBuilder` 创建了一个缓存配置,并使用 `withStatistics(true)` 开启了统计功能。然后,我们使用 `CacheManagerBuilder` 创建了一个缓存管理器,并使用 `withCache` 方法将缓存配置添加到缓存管理器中。最后,我们使用 `build(true)` 方法初始化缓存管理器。
请注意,在这里,我们只为一个缓存名为 "myCache" 的缓存开启了统计功能,如果您希望开启多个缓存的统计功能,可以使用多个 `withCache` 方法。
希望这能帮到您!