25.2. Enabling Spring Cache Support Programmatically (Library Mode)

Spring's cache support can be enabled programmatically in the application. To enable Spring support perform the following steps:
  1. Add the @EnableCaching annotation to the Spring configuration class in use.
  2. Define a method returning a SpringEmbeddedCacheManager annotated with @Bean.
The following code snippet highlights these changes:

Example 25.3. Sample Programmatic Configuration

import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.eviction.EvictionStrategy;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
[...]

@org.springframework.context.annotation.Configuration
@EnableCaching
public class Config {

[...]

    @Bean
    public SpringEmbeddedCacheManager cacheManager() throws Exception {
        Configuration config = new ConfigurationBuilder()
            .eviction()
            .strategy(EvictionStrategy.LRU)
            .maxEntries(150)
            .build();
        
        return SpringEmbeddedCacheManager(new DefaultCacheManager(config));
    }
[...]