Spring in action读书笔记(十) 使用NoSQL数据库之Redis
使用Redis存取数据,首先需要连接到Redis
1、连接到Redis
spring-data-redis-2.1.0.RELEASE包中提供了两个Redis连接工厂,用于生成Redis数据库服务器的连接:
JedisConnectionFactory
LettuceConnectionFactory
具体区别不清楚~~,这个使用JedisConnectionFactory
配置RedisConnectionFactory
@Bean public RedisConnectionFactory factory(RedisStandaloneConfiguration redisStandaloneConfiguration) { return new JedisConnectionFactory(redisStandaloneConfiguration); } @Bean public RedisStandaloneConfiguration redisStandaloneConfiguration() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName("localhost"); //configuration.setPassword(""); configuration.setPort(6379); return configuration; }
2、使用RedisTemplate、StringRedisTemplate
1、基于特定类型的RedisTemplate
定义一个实体类Student
public class Student { public Student(int id, String name) { this.id = id; this.name = name; } public Student() { } private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return id == student.id && name.equals(student.name); } @Override public int hashCode() { return Objects.hash(id, name); } @Override public String toString() { return super.toString(); } }
配置RedisTemplate<String, Student>
@Bean public RedisTemplate<String, Student> studentRedisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, Student> redis = new RedisTemplate<>(); redis.setConnectionFactory(cf); redis.setKeySerializer(new StringRedisSerializer()); redis.setValueSerializer(new Jackson2JsonRedisSerializer<>(Student.class)); return redis; }
注入RedisTemplate<String, Student>并测试
@Autowired private RedisTemplate<String, Student> studentTemplate; @Test public void testStudentTemplate() { String key = "student"; studentTemplate.delete(key); Student student = new Student(0, "Aa"); studentTemplate.opsForSet().add(key, student); Set<Student> set = studentTemplate.opsForSet().members(key); Assert.assertTrue(set != null && set.contains(student)); }
2、基于Object类型的RedisTemplate
配置RedisTemplate<String, Object>
@Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }
注入RedisTemplate<String, Object>并测试
@Autowired private RedisTemplate<String, Object> objectRedisTemplate; @Test public void testObjectTemplate() { String key = "object"; objectRedisTemplate.delete(key); //存取单个实例 Student student = new Student(1, "Bb"); objectRedisTemplate.opsForValue().set(key, student); Object result = objectRedisTemplate.opsForValue().get(key); Assert.assertEquals(student, result); //存取集合类 List<Student> list = new ArrayList<>(); for (int i = 0; i < 3; i++) { list.add(new Student(i, "name" + i)); } objectRedisTemplate.delete("list"); objectRedisTemplate.opsForList().leftPushAll("list", list); List<Object> resultList = objectRedisTemplate.opsForList().range("list", 0, -1); //取出来的数据外面包了一层List if (resultList != null && ((List) resultList).size() > 0 && resultList.get(0) instanceof List) { resultList = (List) resultList.get(0); Assert.assertEquals(list, resultList); } else { Assert.fail(); } }
3、 StringRedisTemplate
StringRedisTemplate配置
@Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory cf) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(cf); stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); stringRedisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); return stringRedisTemplate; }
注入StringRedisTemplate并测试
@Test public void testStringRedisTemplate() throws Exception { String key = "stringRedisTemplate"; stringRedisTemplate.delete(key); Student student = new Student(1, "Bb"); ObjectMapper objectMapper = new ObjectMapper(); String studentString = objectMapper.writeValueAsString(student); //写入、读取单个值 stringRedisTemplate.opsForList().leftPush(key, studentString); String result = stringRedisTemplate.opsForList().leftPop(key); Assert.assertEquals(result, studentString); List<Student> studentList = new ArrayList<>(); Map<String, Student> studentMap = new HashMap<>(); for (int i = 0; i < 5; i++) { Student temp = new Student(i, "name" + i); studentList.add(temp); studentMap.put("" + i, temp); } //存取ArrayList<Student> stringRedisTemplate.delete("studentList"); stringRedisTemplate.opsForValue().set("studentList", objectMapper.writeValueAsString(studentList)); String studentListString = stringRedisTemplate.opsForValue().get("studentList"); JavaType javaType1 = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Student.class); List<Student> studentList1 = objectMapper.readValue(studentListString, javaType1); Assert.assertEquals(studentList, studentList1); //存取Map stringRedisTemplate.delete("studentMap"); stringRedisTemplate.opsForValue().set("studentMap", objectMapper.writeValueAsString(studentMap)); JavaType javaType2 = objectMapper.getTypeFactory().constructParametricType(HashMap.class, String.class, Student.class); Map<String, Student> resultMap = objectMapper.readValue(stringRedisTemplate.opsForValue().get("studentMap"), javaType2); Assert.assertEquals(studentMap.entrySet(), resultMap.entrySet()); }