Redis
基于內(nèi)存進(jìn)行存儲(chǔ)喷斋,支持 key-value 的存儲(chǔ)形式,底層是用 C 語(yǔ)言編寫的蘑辑。
基于 key-value 形式的數(shù)據(jù)字典,結(jié)構(gòu)非常簡(jiǎn)單坠宴,沒(méi)有數(shù)據(jù)表的概念洋魂,直接用鍵值對(duì)的形式完成數(shù)據(jù)的管理,Redis 支持 5 種數(shù)據(jù)類型:
- 字符串
- 列表
- 集合
- 有序集合
- 哈希
安裝 Redis
1喜鼓、下載 Redis
2副砍、解壓,并在本地硬盤任意位置創(chuàng)建文件夾颠通,在其中創(chuàng)建 3 個(gè)子文件夾
- bin:放置啟動(dòng) Redis 的可執(zhí)行文件
- db:放置數(shù)據(jù)文件
- etc:放置配置文件址晕,設(shè)置 Redis 服務(wù)的端口、日志文件位置顿锰、數(shù)據(jù)文件位置...
啟動(dòng) Redis 服務(wù)
1谨垃、進(jìn)入 redis 目錄启搂,啟動(dòng) redis-server。
sudo ./bin/redis-server ./etc/redis.conf
2刘陶、進(jìn)入 redis 目錄胳赌,啟動(dòng) redis-cli,啟動(dòng) Redis 的客戶端管理窗口匙隔,在此窗口中即可操作 Redis 數(shù)據(jù)庫(kù)疑苫。
./bin/redis-cli
3、對(duì)數(shù)據(jù)進(jìn)行操作纷责。
set key value
get key
4捍掺、關(guān)閉 Redis 服務(wù)。
shutdown
5再膳、退出客戶端挺勿,control+c。
Spring Boot 整合 Redis
Spring Data Redis 操作 Redis喂柒。
1不瓶、創(chuàng)建 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灾杰、創(chuàng)建實(shí)體類蚊丐,實(shí)現(xiàn)序列化接口,否則無(wú)法存入 Redis 數(shù)據(jù)庫(kù)艳吠。
package com.southwind.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麦备、創(chuàng)建控制器。
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
@RestController
public class StudentHandler {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
public void set(@RequestBody Student student){
redisTemplate.opsForValue().set("student",student);
}
@GetMapping("/get/{key}")
public Student get(@PathVariable("key") String key){
return (Student) redisTemplate.opsForValue().get(key);
}
@DeleteMapping("/delete/{key}")
public boolean delete(@PathVariable("key") String key){
redisTemplate.delete(key);
return redisTemplate.hasKey(key);
}
}
4讲竿、創(chuàng)建配置文件 application.yml
spring:
redis:
database: 0
host: localhost
port: 6379
5泥兰、創(chuàng)建啟動(dòng)類
package com.southwind;
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 種數(shù)據(jù)類型
字符串
@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","World");
listOperations.leftPush("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;
}
哈希
HashMap key value
HashOperations key hashkey value
key 是每一組數(shù)據(jù)的 ID弄屡,hashkey 和 value 是一組完整的 HashMap 數(shù)據(jù)题禀,通過(guò) key 來(lái)區(qū)分不同的 HashMap。
HashMap hashMap1 = new HashMap();
hashMap1.put(key1,value1);
HashMap hashMap2 = new HashMap();
hashMap2.put(key2,value2);
HashMap hashMap3 = new HashMap();
hashMap3.put(key3,value3);
HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
hashOperations.put(hashMap1,key1,value1);
hashOperations.put(hashMap2,key2,value2);
hashOperations.put(hashMap3,key3,value3);