springboot how data is stored in the cache to redis

package com.hope;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hope.domain.User;
import com.hope.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
* @author newcityman
* @date 2020/1/19 - 23:20
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaApplication.class)
public class RedisTest {
@Autowired
private RedisTemplate<String, String> redisTemplate;

@Autowired
private UserRepository userRepository;
@Test
public void test() throws JsonProcessingException {
//1、从redis中获取数据,数据形式是json
String userListJson = redisTemplate.boundValueOps("user.findAll") GET (). ; // 2, determines whether the present redis IF ( null == userListJson) { //. 3, if there is, from the database query List <the User> List = userRepository.findAll ( ) ; // 4, the isolated data is stored in the redis ObjectMapper ObjectMapper = new new ObjectMapper () ; userListJson = objectMapper.writeValueAsString (List) ; redisTemplate.boundValueOps ( . "user.findAll") SET (userListJson) ; the System. Out.println ( "======== obtain data from the database stored in the buffer ========") ; } the else { . the System Out.println ( "======== data retrieved from the cache redis ======== ") ; } // 5, the print data in the console System.














out.println(userListJson);
}
}

Guess you like

Origin www.cnblogs.com/newcityboy/p/12216029.html