SpringBoot 整合 springDataRedis
一、添加依赖:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-redis</artifactId> 4 </dependency>
二、redis 配置:
1 #服务器数据库 2 spring.redis.database=0 3 4 #ip地址 5 spring.redis.host=localhost 6 7 #端口号 8 spring.redis.port=6379 9 10 #密码 11 spring.redis.password= 12 13 #客户端超时时间(毫秒),默认是2000 14 spring.redis.timeout=6000 15 16 #Redis 连接池 17 #连接池的最大连接数(0表示没有限制) 18 spring.redis.jedis.pool.max-active=100 19 20 #连接池最大阻塞等待时间(0表示没有限制) 21 spring.redis.jedis.pool.max-wait=-1 22 23 #连接池中最大空闲连接 24 spring.redis.jedis.pool.max-idle=10 25 26 #连接池中最小空闲连接 27 spring.redis.jedis.pool.min-idle=0
三、RedisTemplate & StringRedisTemplate:前者默认使用JDKSerializationRedisSerializer序列化key-value,后者使用StringRedisSerializer序列化key-value。注意两者的数据不共享,只能管理自己的数据。StringRedisTemplate 继承了 RedisTemplate 设置了不同的序列化策略。
1、RedisTemplate:
1 public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V>, BeanClassLoaderAware { 2 private boolean enableTransactionSupport = false; 3 private boolean exposeConnection = false; 4 private boolean initialized = false; 5 private boolean enableDefaultSerializer = true; 6 ... 7 public void afterPropertiesSet() { 8 super.afterPropertiesSet(); 9 boolean defaultUsed = false; 10 if (this.defaultSerializer == null) { 11 this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader()); 12 } 13 ... 14 } 15 ... 16 17 }
2、StringRedisTemplate:
1 public class StringRedisTemplate extends RedisTemplate<String, String> { 2 public StringRedisTemplate() { 3 this.setKeySerializer(RedisSerializer.string()); 4 this.setValueSerializer(RedisSerializer.string()); 5 this.setHashKeySerializer(RedisSerializer.string()); 6 this.setHashValueSerializer(RedisSerializer.string()); 7 } 8 ... 9 }
四、RedisTemplate & StringRedisTemplate 实战:
1、RedisTemplate & StringRedisTemplate :使用默认的序列化策略,在存储上的差异
1 @RestController 2 public class RedisTemplateStringRedisTemplateDemo { 3 4 @Autowired 5 private RedisTemplate redisTemplate; 6 7 @Autowired 8 private StringRedisTemplate stringRedisTemplate; 9 10 @RequestMapping("/user/{id}") // id = 001 11 public String getUserById(@PathVariable(required = false,name = "id") String id){ 12 Boolean keyExists = redisTemplate.hasKey(id); 13 User user = null; //User.calss implements Serializable 支持序列化 14 if(keyExists){ 15 user = (User)redisTemplate.opsForValue().get(id);// 获取对象 16 String userStr = stringRedisTemplate.opsForValue().get(id); //获取字符串 17 System.out.println("userStr" + userStr); 18 }else{ 19 user = new User(); 20 user.setAge("18"); 21 user.setName("cxy"); 22 user.setSex("男"); 23 redisTemplate.opsForValue().set(id, user);//默认使用JDKSerializationRedisSerializer序列化来存储key-value 24 stringRedisTemplate.opsForValue().set(id, user.toString());//使用StringRedisSerializer序列化存储key-value,所以在服务器中存储的是字符串 25 } 26 return user.toString(); 27 } 28 29 }
2、自定义序列化策略:
1 @Configuration 2 public class MyRedisTemplateConfig { 3 4 @Bean("strRedisTemplate") 5 public RedisTemplate<Object, Object> strRedisTemplate(RedisConnectionFactory redisConnectionFactory){ 6 RedisTemplate<Object,Object> template = new RedisTemplate<Object, Object>(); 7 template.setConnectionFactory(redisConnectionFactory); 8 RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer(); 9 template.setKeySerializer(stringRedisSerializer);// 修改key的存储方式为String 10 return template; 11 } 12 }
1 @RestController 2 public class RedisTemplateStringRedisTemplateDemo { 3 4 @Autowired 5 @Qualifier("strRedisTemplate") 6 private RedisTemplate redisTemplate; 7 8 //@Autowired 9 //private StringRedisTemplate stringRedisTemplate; 10 11 @RequestMapping("/user/{id}") 12 public String getUserById(@PathVariable(required = false,name = "id") String id){ 13 Boolean keyExists = redisTemplate.hasKey(id); 14 User user = null; 15 if(keyExists){ 16 user = (User)redisTemplate.opsForValue().get(id); 17 // String userStr = stringRedisTemplate.opsForValue().get(id); 18 //System.out.println("userStr" + userStr); 19 }else{ 20 user = new User(); 21 user.setAge("18"); 22 user.setName("cxy"); 23 user.setSex("男"); 24 redisTemplate.opsForValue().set(id, user); 25 //stringRedisTemplate.opsForValue().set(id, user.toString()); 26 } 27 return user.toString(); 28 } 29 30 }