データはRedisのにキャッシュに格納されているかspringboot

パッケージcom.hope 

輸入com.fasterxml.jackson.core.JsonProcessingException
輸入com.fasterxml.jackson.databind.ObjectMapper ;
輸入com.hope.domain.User ;
輸入com.hope.repository.UserRepository
輸入org.junit。テスト;
輸入org.junit.runner。RunWith ;
輸入org.springframework.beans.factory.annotation。Autowired ;
org.springframework.boot.test.contextインポートします。SpringBootTest ;
輸入org.springframework.data.redis.core.RedisTemplate ;
輸入org.springframework.test.context.junit4.SpringRunner ;

輸入はjava.util.List ;

/ **
* @author newcityman
* @date 2020年1月19日-夜11時20分
* /
@RunWith(SpringRunner。クラス)
@SpringBootTest(クラス= SpringbootJpaApplication。クラス)
publicクラスRedisTest {
@Autowired プライベートRedisTemplate <文字列、文字列> redisTemplate ; @Autowired プライベートUserRepository userRepository @Test 公共ボイド試験()JsonProcessingException {スロー// 1、从Redisの中获取数据、数据形式是JSON 文字列userListJson = redisTemplate.boundValueOpsを(







"user.findAll")GET(); // 2は、本Redisのか否かを判断する(IF {ヌル== userListJson)// 3、データベースクエリから、存在する場合。一覧<ユーザ>リスト= userRepository.findAll( ); // 4は、単離されたデータはRedisの中に格納されObjectMapper ObjectMapper = 新しい新しいObjectMapper(); userListJson = objectMapper.writeValueAsString(一覧); redisTemplate.boundValueOps("user.findAll")SET(userListJson); システム。out.printlnを(「======== ========バッファに格納されたデータベースからデータを取得」); } {他システム。out.printlnを(「========キャッシュRedisの========「から検索されたデータ); } // 5、コンソールの印刷データシステム。














out.printlnを(userListJson)
}
}

おすすめ

転載: www.cnblogs.com/newcityboy/p/12216029.html