How to avoid creation of Multiple Method Interceptors for the method with cache annotations when using spring data jpa and spring data redis

跟風遠走 提交于 2019-12-11 04:10:43

问题


In my application I am using two data modules (spring-boot-starter-data-jpa and spring-boot-starter-data-redis). I have a method annotated with @CachPut to store in cache. this method is called once but the actual CachePut operation is happening twice. when I debug there are two proxies created which intercepts the same method.

When I use only one module spring-boot-starter-data-redis it works as expected because there is only one proxy created which intercepts the method having @CachPut annotation.

But as per our requirement our application need to use both the data modules (spring-boot-starter-data-jpa for DB related stuff and spring-boot-starter-data-redis for handling some cache related stuff). If I add spring-boot-starter-data-jpa then the cache operation is getting executed twice (due to multiple proxies).

Is there any way to disable proxy creation with @EnableCaching for jpa module but enable only for redis module.

Code Snippets

Configuration class:

@Configuration

@EnableCaching

public class MyConfiguration {
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory, @NotNull RedisCacheProperties properties) {
        ....
    }
}

Dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>

CacheProcessor (Interface and Implementation classes)

public interface MyCacheProcessor {
    MyData store(final MyData myData);
}
public class MyCacheProcessorImpl implements MyCacheProcessor {
    @CachePut(cacheNames = {"my-data"}, key = "#myData.id")
    public MyData store(final MyData myData) {
        log.debug("storing in redis"); // This is printed only once (but actual cache put operation is happening twice )
        return myData;
    }
}

yaml configs

spring:

    redis:
        timeout: 500
        host: devserver
        port: 26379
    datasource:
        url: jdbc:h2:mem:testdb;Mode=Oracle;DB_CLOSE_ON_EXIT=FALSE
        platform: h2

redis.cache.enabled: true

application:

redis:
    cache:
      my-data:
        ttl: 15m
        serializer:
        type: org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
        handles: com.my.sample.model.MyData

I expect the cache operation should be executed only once even If I use both the data modules. Currently the cache operation is executed twice.

Any guidance would be appreciated.

来源:https://stackoverflow.com/questions/57549865/how-to-avoid-creation-of-multiple-method-interceptors-for-the-method-with-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!