java - 从 GuavaCache 迁移到 EhCache (Spring Boot)

标签 java spring caching migration ehcache

我正在尝试更改以下方法以使用 EhCache 缓存而不是 Guava 缓存,因为 Guava 缓存已从 Spring 5.0 中删除。网上似乎没有任何关于如何简单地实例化 EhCacheCache 对象并将其传递到 SimpleCacheManager 的文档。我如何实现这一目标?

@Configuration
@EnableCaching
public class CacheConfig {

  @Bean
  public CacheManager cacheManager() {

    GuavaCache a =
        new GuavaCache("a", CacheBuilder.newBuilder().maximumSize(10000)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    GuavaCache b =
        new GuavaCache("b", CacheBuilder.newBuilder().maximumSize(100)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    GuavaCache c =
        new GuavaCache("c", CacheBuilder.newBuilder().maximumSize(100)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    GuavaCache d =
        new GuavaCache("d", CacheBuilder.newBuilder().maximumSize(20)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    GuavaCache e =
        new GuavaCache("e", CacheBuilder.newBuilder().maximumSize(100)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    GuavaCache f =
        new GuavaCache("f", CacheBuilder.newBuilder().maximumSize(10)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    GuavaCache g =
        new GuavaCache("g", CacheBuilder.newBuilder().maximumSize(5000)
            .expireAfterAccess(24, TimeUnit.HOURS).recordStats().build());

    SimpleCacheManager simpleCacheManager = new SimpleCacheManager();

    simpleCacheManager.setCaches(
      Arrays.asList(
        a,
        b,
        c,
        d,
        e,
        f,
        g
      )
    );

    return simpleCacheManager;
  }
}

最佳答案

事情不是这样的。假设您将使用 Ehcache 3.x,它符合 JSR107。因此您将使用JCacheCacheManager。当看到 jcache api 在类路径中可用时,Spring-boot 将对其进行配置,而不执行任何操作。

事实上,最简单的方法通常是放手去做并使用JCacheManagerCustomizer来添加您想要的缓存。就像下面这样。

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            cm.createCache("a", createConfiguration(100, Duration.ofHours(24)));
        };
    }

    private javax.cache.configuration.Configuration<Object, Object> createConfiguration(long size, Duration tti) {
        return Eh107Configuration.fromEhcacheCacheConfiguration(
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
                ResourcePoolsBuilder.heap(size))
                .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(tti))
                .build());
    }
}

关于java - 从 GuavaCache 迁移到 EhCache (Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51599672/

相关文章:

java - 给出索引 4 处的绑定(bind)值为 null 的错误

android - picasso 不在磁盘上缓存图像

Django 使用多个redis进行缓存

java - 将字节 [] 转换为整数

java - ISO 8601 DateTimeFormatter 截断此格式的 ms :'YYYY-MM-DDTHH:mm:ss.sssZ'

java - 打开自定义枚举值的值

spring - 将原型(prototype) bean 注入(inject)单例 bean

java - Spring安全并发 session : failed to make "max-sessions" field configurable

java - 如何使用 Spring JmsTemplate 更改 MQ header

html - 是否有一个 &lt;meta&gt; 标签来关闭所有浏览器中的缓存?