1

我刚刚开始使用咖啡因。我看到CaffeineCacheSpring Boot 中的类只支持CacheCaffeine 的接口实现,如下面的代码所示,但我想知道 Spring Boot 是否支持实现AsyncCache?谢谢

@Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(caffeineCacheBuilder());
        cacheManager.setAllowNullValues(false);
        return cacheManager;
    }

 @NonNull Caffeine<Object, Object> caffeineCacheBuilder() {
        return Caffeine.newBuilder()
            .initialCapacity(100)
            .maximumSize(500)
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .recordStats()
            .removalListener(new RemovalListener<Object, Object>() {
                @Override
                public void onRemoval(@Nullable Object key, @Nullable Object value, @NonNull RemovalCause cause) {
                    System.out.println("Remove listener: " + key.toString());
                }
            })
            .writer(new CacheWriter<Object, Object>() {
                @Override
                public void write(@NonNull Object key, @NonNull Object value) {
                    System.out.println("Written: " + key.toString());
                }

                @Override
                public void delete(@NonNull Object key, @Nullable Object value, @NonNull RemovalCause cause) {
                    System.out.println("Deleted: " + key.toString());
                }
            });
    }
4

0 回答 0