0004SpringBoot integrate Redis

It has been integrated SpringDataJPA and Junit based on the integration of Redis, only need to look at a few steps:

1, download 64windows version of Redis installation package, unzip and start the server

2, the configuration dependent Redis start (the pom.xml)

3, the configuration information (application.propertis) server connected Redis

4, write test classes

5, start the test

Details are as follows:

Download 64windows version of Redis installation package, unzip and start the server

Download address is:

https://github.com/MicrosoftArchive/redis/releases

Download a 64-bit version, as shown below:

 

After extracting:

Double-click to start the redis redis-server.exe server

Double-click to start redis redis-cli.exe client

pom.xml starting configuration Redis dependency:

<!--redis的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.properties redis connection configuration information server:

# Redis server address 
spring.redis.host = localhost
# Redis server port
spring.redis.port = 6379

Write test classes:

package com.myself;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myself.domain.User;
import com.myself.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;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaApplication.class)
public class RedisTest {
@Autowired
RedisTemplate Private <String, String> redisTemplate;

@Autowired
Private UserRepository userRepository;

@Test
public void queryUsers () throws JsonProcessingException {
// redis query data from the cache, the nosql is redis KV value pairs, the bond is set user.findAll
        RedisTemplate.boundValueOps listUserJson = String ( "user.findAll") GET ();. 
// If the data is empty, for the first visit, you need to query data from the database
IF (listUserJson == null) {
       // query from the database data
List <the User> Users userRepository.findAll = ();
// json to convert the data string, since we rely on the configuration of the starting web, so we can use the data conversion Jackson
       // jackson objects in
ObjectMapper objectMapper = new ObjectMapper ();
listUserJson = objectMapper.writeValueAsString (Users);
redisTemplate.boundValueOps ( "user.findAll") SET (listUserJson);.
System.out.println ( "============== === query data from the database ========================================== === ");
} the else {
System.out.println ( "================= ==================== query data from the cache redis ========================= ");
}
System.out.println (" User data: "+ listUserJson);

}

}

if appreciated not in place of, hope correct me.

 

Guess you like

Origin www.cnblogs.com/xiao1572662/p/11876239.html