Redis curd and access to various data types

1. Redis use process

This is the steps to use Springboot to integrate redis

(1) Import redis dependency in pom.xml

<!--导入这个redis依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

(2) Create entity classes and implement serialization interfaces

If the serialization interface is not implemented, it cannot be stored in the redis database

Insert picture description here


(3) Redistemplate is used in the Controller (4) Redis

Insert picture description here


Insert picture description here


Insert picture description here


connection information is configured in yml

There are a total of 16 redis libraries, where 0 means the first library is selected by default

Insert picture description here


(5) Use postman for testing

Insert picture description here


(6) The student saved by get

Insert picture description here


can see that it is nil (that is, empty) at this time. Why?
Because redis will be serialized first when storing the data, that is, a bunch of things will be added in front of the student to be used as a mark

Insert picture description here


Insert picture description here


(7) get the data in redis in the contorller

Insert picture description here


test, and it will be successfully taken out

Insert picture description here

2. Additions, deletions, modifications

 @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 增/改
     * @param student
     */
    @PostMapping("/set")
    public void set(@RequestBody Student student){
        redisTemplate.opsForValue().set("student",student);
    }

    /**
     * 查
     * @param key
     * @return
     */
    @GetMapping("get/{key}")
    public Student get(@PathVariable("key") String key){
         return (Student) redisTemplate.opsForValue().get(key);
    }

    /**
     * 删
     * @param key
     * @return
     */
    @DeleteMapping("delete/{key}")
    public boolean delete(@PathVariable("key") String key){
        //删除
        redisTemplate.delete(key);
        //寻找redis中是否有key,返回一个boolean值
        return redisTemplate.hasKey(key);
    }

3. Access to various data types of redis

(1) String

  /**
     * String类型数据的存入和取出
     */
    @GetMapping("/string")
    public void getString(){
        //存
        redisTemplate.opsForValue().set("str","hello,redis");
        //取
        String str = (String) redisTemplate.opsForValue().get("str");
        System.out.println(str);
    }

(2) List

  /**
     * List类型数据的存取
     * @return
     */
 @GetMapping("/getList")
    public List<String> getList(){
        //ListOperations相当于一根管道,左右两边都可以存取数据
        ListOperations<String,String> listOperations = redisTemplate.opsForList();
        //存
        listOperations.leftPush("list","hello");
        listOperations.rightPush("list","redis");
        //使用range进行取,后面的两个参数类似于subString()的两个参数 指定下标获取
        return listOperations.range("list",0,0); //hello
    }
Insert picture description here

(3) Set

① Unordered Set

 /**
     * 无序Set的存取
     */
    @GetMapping("/getSet")
    public void getSet(){
        SetOperations<String,String> setOperations = redisTemplate.opsForSet();
        //存
        setOperations.add("set","hello");
        setOperations.add("set","redis");
        //取
        Set<String> str = setOperations.members("set");
    }

② Ordered Set

/**
     * 有序Set的存取
     */
    @GetMapping("/getZSet")
    public void getZSet(){
        ZSetOperations<String,String> zsetOperations = redisTemplate.opsForZSet();
        //存,第三个参数为序号 redis中会根据序号对set集合排序
        zsetOperations.add("zset","hello",1);
        zsetOperations.add("zset","redis",2);
        //取
        Set<String> str = zsetOperations.range("zset",0,1);
        System.out.println(str);
    }

(4) HashMap

 /**
     * HashMap的存取
     */
    @GetMapping("/getHash")
    public void getHash(){
        //3个参数说明:hashMap的id,hashMap的key和value
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        //存
        hashOperations.put("hashMap","hashKey","hello,redis");
        //取
        String str = hashOperations.get("hashMap","hashKey");
        System.out.println(str);
    }