标签:dal ali temp size dex redis return etc frame
首先是连接池的选择 一般有两种 lettuce ,jedis
Jedis 线程不安全,方法同步
Lettuce 基于Netty.nio, 方法异步 线程 安全
letture通过引入spring-boot-starter-redis就可以使用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.0.1.RELEASE</version> </dependency>
而Jedis需要在这基础上引入
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <type>jar</type> </dependency>
可以同时配置多个RedisConnectionFactory
多个factory需要标注Primary 未标注的Factory系统会调用 StringRedisTemplate
@RestController public class MyController { @Autowired @Qualifier("stringRedisTemplate") private StringRedisTemplate lettuceTemplate; @Autowired @Resource(name="jedisTemplate") private RedisTemplate jedisTemplate; @RequestMapping("/") public String index() { return "hello "+lettuceTemplate.opsForValue().get("name"); } @RequestMapping("/set") public String index(@RequestParam String name) { jedisTemplate.opsForValue().set("name",name,Duration.ofMillis(5000)); return "success "; } // @Bean("jedisTemplate") public RedisTemplate<String, Object> jedisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); return template; } @Bean("lettuceTemplate") public RedisTemplate<String, Object> lettuceTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(lettuceConnectionFactory()); return template; } @Primary @Bean("jedisConnectionFactory") public RedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName("127.0.0.1"); config.setPort(6379); config.setPassword("123456"); return new JedisConnectionFactory(config); } /** * Lettuce */ @Bean("lettuceConnectionFactory") public RedisConnectionFactory lettuceConnectionFactory() { System.out.println("lettuceConnectionFactory "); RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName("127.0.0.1"); config.setPort(6379); config.setPassword("123456"); return new LettuceConnectionFactory(config); } }
标签:dal ali temp size dex redis return etc frame
原文地址:https://www.cnblogs.com/dikeboy/p/10080653.html