本文主要是介绍springboot中redisTemplate指定使用DB,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
可以在初始化redisTemplate时指定,如
@Bean("redisTemplateDB1")public RedisTemplate<String, String> redisTemplateDB1(@Value("${spring.redis.redis-b.host}") String host, @Value("${spring.redis.redis-b.port}") int port, @Value("${spring.redis.redis-b.database}") int db, @Value("${spring.redis.redis-b.password}") String password) {RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();redisConfig.setHostName(host);redisConfig.setPassword(password);redisConfig.setPort(port);//指定databaseredisConfig.setDatabase(db);GenericObjectPoolConfig config=new GenericObjectPoolConfig();LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();LettuceConnectionFactory lettuceConnectionFactory=new LettuceConnectionFactory(redisConfig, clientConfiguration);RedisTemplate<String, String> template = new RedisTemplate<>();// 配置连接工厂template.setConnectionFactory(lettuceConnectionFactory);redisConfig tetemplate;}
也可以在使用前指定,像这样
LettuceConnectionFactory jedisConnectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();//指定dbjedisConnectionFactory.setDatabase(1);redisTemplate.setConnectionFactory(jedisConnectionFactory);
这篇关于springboot中redisTemplate指定使用DB的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!