nodejs 同步執行非同步函式
阿新 • • 發佈:2020-11-26
技術標籤:redisspring bootspringideaintellij idea
redis是基於記憶體進行儲存,支援key-value的儲存形式,底層是用C語言寫的。
基於key-value形式的資料字典,結構非常簡單,沒有資料表的概念,直接用鍵值對的形式完成資料的管理,支援5種資料型別:
字串
列表
集合
有序集合
雜湊
Spring Boot 整合 Redis
實際上使用Spring Data Redis操作Redis,
1、建立maven工程
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com. hhzhu</groupId>
<artifactId>redis_practice</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
</project>
2、建立實體類,實現序列化介面。
package com.hhzhu.pojo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* Description:
*
* @author Yang
* @data Create on 2020/12/17
*/
@Data
//實現序列化介面,否則無法存入redis
public class Student implements Serializable {
private Integer id;
private String name;
private Double score;
private Date birthday;
}
3、建立控制器
package com.hhzhu.controller;
import com.hhzhu.pojo.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;
/**
* Description:
*
* @author Yang
* @data Create on 2020/12/17
*/
@RestController
public class StudentHandler {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
//Request將json資料轉換成java物件
public void set(@RequestBody Student student){
redisTemplate.opsForValue().set("student",student);
}
}
4、建立配置檔案appliacation.yml
spring:
redis:
database: 0
host: localhost
port: 6379
5、建立啟動類
package com.hhzhu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Description:
*
* @author Yang
* @data Create on 2020/12/17
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
6、CRUD
package com.hhzhu.controller;
import com.hhzhu.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
/**
* Description:
*
* @author Yang
* @data Create on 2020/12/17
*/
@RestController
public class StudentHandler {
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("/set")
//Request將json資料轉換成java物件
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);
}
}
Redis 5種資料型別
字串
@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;
}
集合(set)
@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未命名檔案
HashMap hashMap = new HashMap();
hashMap.put(key1,value1);
HashMap hashMap = new HashMap();
hashMap.put(key2,value2);
HashMap hashMap = new HashMap();
hashMap.put(key3,value3);
HashOperations<String,String,String> hashOperation = redisTemplate.opsForHash();
hashOperation.put(hashMap1,key1,value1);
hashOperation.put(hashMap2,key2,value2);
hashOperation.put(hashMap3,key3,value3);
@GetMapping("/hash")
public void hashTest(){
HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
hashOperations.put("key","hashKey","Hello");
System.out.println(hashOperations.get("key","hashKey"));
}