简单说一下Redis的安装运行

  1. 下载并解压redis压缩包到本地(我是在github上找的windows版本,redis下载链接
  2. 在安装目录打开cmd窗口(可以在地址栏输入cmd快捷打开),输入redis-server redis.windows.conf来启动redis
    image.png
  3. redis命令的简单使用
  4. image.png

Redis的持久化RDB与AOF

image.png

SpringBoot整合Redis

SpringBoot操作Redis之经典CRUD

添加键值对时若原先有key值则覆盖,相当于更新

  1. 创建maven工程并导入相关pom.xml依赖
<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.1.5.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!--连接池-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
  </dependencies>
  1. 创建spring的配置文件application.yml
spring:
  data:
    redis:
      database: 0
      host: localhost
      port: 6379
  1. 创建实体类并使其实现序列化接口!
@Data
public class Student implements Serializable {
    private String name;
    private  int id;
    private  String sex;
}

  1. 创建controller类实现增删改查
    这里删除操作不需要调用opsForValue
@RestController
@RequestMapping("/student")
public class StudentHandler {
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/set")
    public void set(@RequestBody Student student){
        redisTemplate.opsForValue().set("student1",student);
        System.out.println("添加成功!!!");
    }
    @GetMapping("/get/{key}")
    public void get(@PathVariable String key){
        Student student = (Student) redisTemplate.opsForValue().get(key);
        System.out.println(student);
    }
    @DeleteMapping("/delete/{key}")
    public void delete(@PathVariable String key){
        //这里删除操作不需要调用opsForValue
        redisTemplate.delete(key);
        System.out.println("删除成功!");
    }
}

  1. 创建springboot启动类
@SpringBootApplication

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

  1. 通过RestService结合Redis测试
    在测试前记得打开redis-service

image.png
image.png
idea后台输出截图:
image.png

对Redis的五种数据结构进行操作

Redis有五种数据结构
分别为字符串、哈希、集合、有序集合、列表

字符串:

@GetMapping("/string")
public String stringTest(){
    redisTemplate.opsForValue().set("str","Hello world");
    //System.out.println(redisTemplate.opsForValue().get("str"));
    String str = (String) redisTemplate.opsForValue().get("str");
    return str;
}

列表:

@GetMapping("list")
public List<String> listTest(){
    ListOperations<String,String> listOperations = redisTemplate.opsForList();
    listOperations.leftPush("list","hello");
    listOperations.leftPush("list","world");
    listOperations.rightPush("list","java");
  
    List<String> list = listOperations.range("list",0,2);
    return list;
}

集合:

@GetMapping("/set")
public Set<String> setTest(){
    SetOperations<String,String> setOperations = redisTemplate.opsForSet();
    setOperations.add("set","Hello");
    setOperations.add("set","Hello");
    setOperations.add("set","world");
    setOperations.add("set","world");
    setOperations.add("set","java");
    setOperations.add("set","java");
  
    Set<String> set = setOperations.members("set");
    return set;
}

有序集合:

@GetMapping("/zset")
public Set<String> zsetTest(){
    ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
    zSetOperations.add("zset","Hello",1);
    zSetOperations.add("zset","world",2);
    zSetOperations.add("zset","java",3);
  
    Set<String> set = zSetOperations.range("zset",0,2);
    return set;
}

哈希:

Hash:key value
HashOperations:key hashkey value
key是每一组数据的ID,hashkey和value是一组完整的HashMap数据,通过key来区分不同的HashMap

@GetMapping("/hash")
public void hashTest(){
    HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
    hashOperations.put("key","hashKey","Hello");
    System.out.println(hashOperations.get("key","hashKey"));
    
}

Q.E.D.