Redis
-
基于内存进⾏存储,⽀持 key-value 的存储形式,底层是⽤ C 语⾔编写的。
-
基于 key-value 形式的数据字典,结构⾮常简单,没有数据表的概念,直接⽤键值对的形式完成数据的 管理,Redis ⽀持
5种数据类型:
- 字符串
- 列表
- 集合
- 有序集合
- 哈希
Spring Boot 整合 Redis
Spring Data Redis 操作 Redis。
1、创建 Maven ⼯程。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<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>
2、创建实体类,实现序列化接⼝,否则⽆法存⼊ Redis 数据库。
package com.zeng.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class Student implements Serializable {
private Integer id;
private String name;
private Double score;
private Date birthday;
}
3、创建控制器
package com.zeng.controller;
import com.zeng.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentHandler {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
public void set(@RequestBody Student student){
redisTemplate.opsForValue().set("student",student);
}
}
4、创建配置⽂件 application.yml
spring:
redis:
database: 0
host: localhost
port: 6379
5、创建启动类
package com.zeng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
Redis 5 种数据类型
字符串
@GetMapping("/string")
public String stringTest(){
redisTemplate.opsForValue().set("str","Hello World");
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","Hello");
listOperations.leftPush("list","World");
listOperations.leftPush("list","World");
listOperations.leftPush("list","Java");
listOperations.leftPush("list","Java");
List<String> list = listOperations.range("list",0,5);
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",3);
zSetOperations.add("zset","World",1);
zSetOperations.add("zset","Java",2);
Set<String> set = zSetOperations.range("zset",0,2);
return set;
}
哈希
- HashMap 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"));
}