When configuring cache with spring.cache.*
properties, it would be very useful to provide a property to enable cache transaction awaring. I noticed that {EhCache, JCache,Redis}CacheManager
extend AbstractTransactionSupportingCacheManager
which has the boolean transactionAware
property.
Could you add a property like spring.cache.enable-transaction-aware
?
Thanks
Comment From: philwebb
What are your thoughts on this @snicoll?
It looks like Couchbase, EhCache, JCache and Redis are the only implementations that extend AbstractTransactionSupportingCacheManager
. Perhaps we could add such a property under those specific keys (e.g. spring.cache.redis.transaction-aware
).
Comment From: snicoll
I am not sure about that to be honest. This is more about the one-liner vs. changing that property based on the environment as far as I can see and you can achieve that with a CacheManagerCustomizer
. Even in a generic fashion by targetting AbstractTransactionSupportingCacheManager
.
@akuma8 do you have a case where you want to change the "transaction awareness" based on the environment (and/or a profile)?
Comment From: akuma8
@akuma8 do you have a case where you want to change the "transaction awareness" based on the environment (and/or a profile)?
Yes, it's a personnal usage but to save time when trying to validate a specific functionnality during development, I disable cache transaction, I don't care about the failling of other functionnalities, all I want is to have my data (I use Redis), whereas in production transaction is always enabled. I still think that this property should be configurable via application.properties. The Spring Boot dna is to make our developer life easier and with this simple property I defined 2 CacheManager, one for development and another one for prod. I know this is not a priority issue so if I can contribute to add it, please let me know.
Comment From: snicoll
Thanks for the suggestion @akuma8. We haven't seen a lot of demand for this feature and we don't believe that everything has to be exposed via a property, particularly when a customizer is in place. Here is a code snippet that does enable transactions for any CacheManager
that supports the feature:
@Bean
public CacheManagerCustomizer<AbstractTransactionSupportingCacheManager> cacheManagerCustomizer() {
return (cacheManager) -> cacheManager.setTransactionAware(true);
}