1

I have just started working with Caffeine. I see CaffeineCache class in Spring Boot only support implementations of Cache interface from Caffeine as shown in the code below, but I would like to know if Spring Boot supports implementations of AsyncCache? Thanks

@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());
                }
            });
    }
  • Spring Cache's api that providers implement is synchronous, so this wouldn't be available. For advanced usages they recommend using a provider directly and only using their annotation-based caching for simple scenarios. – Ben Manes Mar 30 '20 at 02:46
  • @BenManes sounds good. I am currently using Caffeine as a local cache for a Spring Boot app that has a higher TPS requirements, do you suggest using the Cache or the AsyncCache implementation? I am convinced I should use AsyncCache but would like to know your thoughts? – Avinash Reddy Penugonda Mar 30 '20 at 16:28
  • You would know best. If possible you might want to load test in order to gauge if you’ll meet the performance requirements and profile otherwise. – Ben Manes Mar 30 '20 at 16:32
  • I was thinking of doing the same, thanks for the suggestion! – Avinash Reddy Penugonda Mar 30 '20 at 16:37

0 Answers0