Redis的介绍与使用
NoSQL 主流的产品是 MongoDB、Redis。
Not Only SQL
Redis 是当下主流的非关系型数据库,基于内存进行存储,支持 key-value 的存储形式,底层是用 C 语言实现的。
Redis 相当于一个 key-value 的数据字典,结构非常简单,没有数据表的概念,直接用键值对形式完成数据的管理,Redis 支持 5 种数据类型:
- 字符串
- 列表
- 集合
- 有序集合
- 哈希
下载 Redis
启动 Reids 服务
sudo ../bin/redis-server ./redis.conf
启动 Redis 客户端
../bin/redis-cli
关闭 Redis 服务
shutdown
Spring Boot 整合 Redis
1、pom.xml
<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、创建实体类
package com.m.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、Handler
package com.m.controller;
import com.m.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("stu",student);
}
}
4、application.yml
spring:
redis:
database: 0
host: localhost
port: 6379
5、启动类
package com.m;
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);
}
}
字符串
@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> list = redisTemplate.opsForList();
list.leftPush("list","Hello");
list.leftPush("list","World");
list.leftPush("list","Java");
List<String> result = list.range("list",0,2);
return result;
}
集合
@GetMapping("/set")
public Set<String> setTest(){
SetOperations<String,String> set = redisTemplate.opsForSet();
set.add("set","Hello");
set.add("set","Hello");
set.add("set","World");
set.add("set","World");
set.add("set","Java");
set.add("set","Java");
Set<String> result = set.members("set");
return result;
}
有序集合
@GetMapping("/zset")
public Set<String> zsetTest(){
ZSetOperations<String,String> zset = redisTemplate.opsForZSet();
zset.add("zset","Hello",1);
zset.add("zset","World",2);
zset.add("zset","Java",3);
Set<String> result = zset.range("zset",0,2);
for(String value:result){
System.out.println(value);
}
return result;
}
哈希
HashMap hashMap1 = new HashMap();
hashMap1.put(key1,value1);
HashMap hashMap2 = new HashMap();
hashMap2.put(key2,value2);
HashOperations<String,String,String> hash = redisTemplate.opsForHash();
hash.put(hashMap1,key1,value1);
hash.put(hashMap2,key2,value2);
hashMap1、hashMap2 就是 key,key1 和 key2 就是 hashKey,value1 和 value2 就是 value。
@GetMapping("/hash")
public String hashTest(){
HashOperations<String,String,String> hash = redisTemplate.opsForHash();
hash.put("key","hashkey","hello");
String str = hash.get("key","hashkey");
return str;
}