SpringBoot + Redis simple to use

1. dependence introduction

Added in pom.xml

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

2. Profiles

Redis connection configuration information in application.yml

# Redis数据库索引(默认为0)
# Redis服务器地址
# Redis服务器连接端口
# 连接池最大连接数(使用负值表示没有限制)
# 连接池最大阻塞等待时间(使用负值表示没有限制)
# 连接池中的最大空闲连接
# 连接池中的最小空闲连接
# 连接超时时间(毫秒)
spring:
  redis:
    database: 0
    host: 192.168.88.200
    port: 6379
    jedis:
      pool:
        max-active: 20
        max-wait: -1
        max-idle: 10
        min-idle: 0
    timeout: 1000

3. Use

Create a User entity class

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

@Data
@Accessors(chain = true)
public class User extends JdkSerializationRedisSerializer {

    private Integer id;
    private String name;
}

Use StringRedisTemplate (Key and Value are String), to complete the custom User object stored in the String and redis List data structure

import com.agan.entity.User;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/user")
    public Object addUser(@RequestBody User user){
        stringRedisTemplate.opsForValue().set("user", JSON.toJSONString(user));
        return "success";
    }

    @GetMapping("/user")
    public User getUser(){
        return JSON.parseObject(stringRedisTemplate.opsForValue().get("user"), User.class);
    }

    @PostMapping("/users")
    public Object addUsers(@RequestBody List<User> users){
        stringRedisTemplate.opsForList().rightPushAll("users", users.stream().map(JSON::toJSONString).collect(Collectors.toList()));
        return "success";
    }

    @GetMapping("/users")
    public Object getUsers(){
        List<User> users = new ArrayList<>();
        while (true){
            User user = JSON.parseObject(stringRedisTemplate.opsForList().leftPop("users"), User.class);
            if (Objects.isNull(user)) {
                break;
            }
            users.add(user);
        }
        return users;
    }

}

PS

If you start or call interface error in the project, can not connect prompt Redis, you can do the following configuration redis.conf. Modify the configuration file in the installation directory of redis

the redis.conf

bind 127.0.0.1 to bind 0.0.0.0 represent any connector that allows
protected-mode no protected-mode yes off protection mode to represent

Then after closing redis, use the new configuration file to start redis-server. In Redis directory

src/redis-server redis.conf


Project source code: https: //github.com/AganRun/Learn/tree/master/SpringBoot-Redis

Guess you like

Origin www.cnblogs.com/AganRun/p/11823947.html