Redis的介紹與使用
阿新 • • 發佈:2020-10-16
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;
}