Integrate redis in springboot


One: install redis


honghedeMacBook-Pro: bin honghe $ sudo ./redis-server dynamic redis 


View redis status (open a new terminal)


change Password


Two: integration in springboot

1. Import package

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2. Configure redis

application.xml configuration

spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.port=6379
# Connection timeout - milliseconds
spring.redis.timeout=3000

Code configuration redisTemplate (code configuration is used here)

/**
 * TODO class description
 *
 * @author honghe
 */
@Configuration
public class RedisConfiguration {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;

    /**
     * redis template, storage key is string, value is Jdk serialization
     * @Description:
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        //JdkSerializationRedisSerializer serialization method;
        JdkSerializationRedisSerializer jdkRedisSerializer=new JdkSerializationRedisSerializer();
        redisTemplate.setValueSerializer(jdkRedisSerializer);
        redisTemplate.setHashValueSerializer(jdkRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

Three: test

@RunWith(SpringRunner.class)
@EnableAutoConfiguration
@SpringBootTest
public class SpringbootdemoApplicationTests {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
	@Test
	public void addToRedis() {
        User user = new User();
        user.setSex("男");
        user.setName("admin");
        user.setId(1L);
        stringRedisTemplate.opsForValue().set("user-admin", JSON.toJSONString(user));
        String getRedisByKey = stringRedisTemplate.opsForValue().get("user-admin");
        System.out.println("Query operation after saving: "+getRedisByKey);
	}
    @Test
    public void getByRedis() {
        String getRedisByKey = stringRedisTemplate.opsForValue().get("user-admin");
        System.out.println("Query operation after saving: "+getRedisByKey);
    }
}

terminal view

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[5]> keys *
1) "user-admin"
127.0.0.1:6379[5]> get "user-admin"
"{\"id\":1,\"name\":\"admin\",\"sex\":\"\xe7\x94\xb7\"}"
127.0.0.1:6379[5]>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770536&siteId=291194637