Spring in action reading notes (X) using Redis NoSQL database of

Use Redis access data connection is needed to Redis

1, is connected to the Redis

Providing spring-data-redis-2.1.0.RELEASE package the two Redis connection factory, for generating a connection Redis database server:

JedisConnectionFactory
LettuceConnectionFactory

~~ specific differences is not clear, the use JedisConnectionFactory

Configuration 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, based on the particular type of RedisTemplate

Student entity class defines a

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();
        }
    }
View Code

配置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;
    }

Injection RedisTemplate <String, Student> and tested

@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, based on the type 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 new StringRedisSerializer ();
         // key using the serialization String 
        template.setKeySerializer (stringRedisSerializer);
         // the hash key is also used String serialized manner 
        template.setHashKeySerializer (stringRedisSerializer);
         // value using serialization Jackson 
        template.setValueSerializer (jackson2JsonRedisSerializer);
         // the hash value of the use of serialization Jackson 
        template.setHashValueSerializer (jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

Injection RedisTemplate <String, Object> and tested

@Autowired
    private RedisTemplate<String, Object> objectRedisTemplate;

    @Test
    public  void testObjectTemplate () {
        String key = "object";
        objectRedisTemplate.delete(key);

        // to access a single instance of 
        Student Student = new 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);.   // outside the data taken out of the pack a layer List 
        IF (resultList =! Null && ((List) resultList). size ()> 0 && resultList.get (0) the 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;
    }

And test the injection StringRedisTemplate

@Test
    public void testStringRedisTemplate() throws Exception {

        String key = "stringRedisTemplate";
        stringRedisTemplate.delete(key);

        Student student = new Student(1, "Bb");

        ObjectMapper objectMapper = new ObjectMapper();

        StudentString String = objectMapper.writeValueAsString (Student);
         // write, read a single value 
        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());
    }

Download Project

Guess you like

Origin www.cnblogs.com/wushengwuxi/p/12194868.html