Redis as Hibernate second level cache

后端 未结 6 1098
悲哀的现实
悲哀的现实 2021-01-30 11:19

I have a crazy idea : use Redis as hibernate second cache level. Redis is an in-memory datastore, it supports replication, pipeline, etc.. The goal is to share the hibernate cac

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 11:50

    I used Redisson with Springboot and it's very easy to configure.

    Just add the following:

    implementation 'org.redisson:redisson-spring-boot-starter:3.10.2'
    implementation 'org.redisson:redisson-hibernate-53:3.10.2'
    

    application.yml

    jpa:
      generate-ddl: false
      show-sql: false
      properties:
        hibernate:
          ddl-auto: create
          dialect: com.codefish.model.hibernate.MySqlDialect
          generate_statistics: false
          show_sql: false
          use_sql_comments: false
          format_sql: true
          cache:
            use_second_level_cache: true
            use_query_cache: false
            region.factory_class: com.eddress.config.RegionFactory
    

    RedisConfig

    @Configuration
    public class RedisConfig {
    
    @Value("${spring.redis.master:#{null}}")
    public String redisMaster;
    
    @Value("${spring.redis.slave:#{null}}")
    public String redisSlave;
    
    @Value("${spring.redis.password:#{null}}")
    public String redisPassword;
    
    @Bean
    public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
        return new RedissonConnectionFactory(redisson);
    }
    
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() {
        return Redisson.create(buildConfigFile(redisMaster,redisSlave, redisPassword));
    }
    
    public static Config buildConfigFile(String redisMaster,String redisSlave, String password){
    
        if (Preconditions.isEmpty(redisMaster)) redisMaster = "localhost";
        if (Preconditions.isEmpty(password)) password = null;
    
        Config config = new Config().setTransportMode(TransportMode.NIO).setCodec(new SnappyCodecV2());
    
        if (Preconditions.notBlank(redisSlave)){
            config.useMasterSlaveServers()
                    .setMasterAddress("redis://"+redisMaster+":6379")
                    .addSlaveAddress("redis://"+redisSlave+":6379")
                    .setPassword(password)
                    .setIdleConnectionTimeout(10000)
                    .setTimeout(10000);
        }
        else{
            config.useSingleServer()
                    .setAddress("redis://"+redisMaster+":6379")
                    .setPassword(password)
                    .setIdleConnectionTimeout(10000)
                    .setTimeout(10000);
        }
    
        return config;
    }
    
    @Bean
    CacheManager cacheManager(RedissonClient redissonClient) {
        Map config = new HashMap<>();
        // create "testMap" cache with ttl = 24 minutes and maxIdleTime = 12 minutes
        CacheConfig configItem = new CacheConfig(TimeUnit.MINUTES.toMillis(60), TimeUnit.MINUTES.toMillis(30));
        config.put("domainCache",configItem);
        return new RedissonSpringCacheManager(redissonClient, config);
    }
    

    RegionFactory

    public class RegionFactory extends org.redisson.hibernate.RedissonRegionFactory {
    
        @Override
        protected RedissonClient createRedissonClient(Map properties) {
    
            String master = System.getProperty("REDIS_MASTER");
            String slave = System.getProperty("REDIS_SLAVE");
            String password = System.getProperty("REDIS_PASSWORD");
            Config config = RedisConfig.buildConfigFile(master,slave, password);
            return Redisson.create(config);
    
        }
    
    }
    

提交回复
热议问题