2

I created a cache called "mycache" which is applied to a method in my Service like:

@Cacheable(value = "mycache")
public String getValue(String something) {
   ...breakpoint here...
}

I also have the following in my application.yml:

---
spring:
  profiles: dev
  cache:
    type: NONE

(NOTE: I also tried: none - all lowercase - and that also did not work)

And in my class I have a field that shows me it is the "dev" profile:

@Value("${spring.profiles.active}")
private String activeProfiles;

When I call it the first time (in debug mode), it hits the breakpoint, but everytime after that it does not (which means it's not applying the YAML setting).

How do I disable it by profile?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Have you checked your `env` endpoint to verify that the type is set to `none` at runtime and nothing else happens to be enabling the caches again? – Darren Forsythe Oct 18 '17 at 21:10
  • @DarrenForsythe How would I do that? – Don Rhummy Oct 18 '17 at 21:11
  • the `env` endpoint is part of the Spring Boot Actuator. https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#production-ready-endpoints Note that if you're on `1.5.0` or greater that the endpoints are sensitive by default - to disable `manage.security.disable = false`. Might also want to check the autoconconfiguration report when its set to `none` and without via `debug = true` – Darren Forsythe Oct 18 '17 at 21:15
  • https://github.com/Flaw101/default-boot-app/tree/caching quiok example of the above, see the console logs that the CacheConfiguration(s) do not enable due to cache type being set to none. – Darren Forsythe Oct 18 '17 at 21:20

1 Answers1

4

You can derive your CacheManager by profile as given below:

@Configuration
@EnableCaching
public class CachingConfig {
 
@Bean
@Profile("test")
public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("sampleCache")));
    return cacheManager;
}


@Bean
@Profile("test")
public CacheManager noOpcacheManager() {
final CacheManager   cacheManager = new NoOpCacheManager();
 
return cacheManager;
}
}

In case you want to have conditions on the bean definition, refer here.

andreagalle
  • 620
  • 6
  • 17
surya
  • 2,581
  • 3
  • 22
  • 34