Redis 简介
基于内存进行存储,支持key-value的存储形式,底层是用C语言编写的
基于key-value形式的数据字典,结构非常简单,没有数据表的概念,直接用键值对的形式完成数据的管理,Redis支持5种数据类型:
安装 Redis
下载
https://redis.io/download
解压,并在本地硬盘任意位置创建文件夹,在其中创建3个子文件夹
- bin:放置启动 redis 的可执行文件
- db:放置数据文件
- etc:放置配置文件,设置redis服务的端口、日志文件位置、数据文件位置等
同时将解压后(或者安装后的所有文件)都拷贝到创建的这个文件夹下面
启动redis服务
打开终端,进入到上一步新建的目录下面
先启动server:
./redis-server.exe ./redis.windows.conf
|
当然你也可以将server 注册为一个服务,但是我不建议这样做
再启动 client:
对数据进行操作:
退出client:shutdown
Spring Boot整合Redis
spring data Redis 操作 redis
<?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.example</groupId> <artifactId>springbootredis</artifactId> <version>1.0-SNAPSHOT</version>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.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、创建实体类,必须实现序列化接口,否则无法存入redis缓存数据库
package com.example.entity;
import lombok.Data;
import java.io.Serializable; import java.util.Date;
@Data public class Student implements Serializable {
private String name;
private Integer id;
private Double score;
private Date birthday; }
|
package com.example.controller;
import com.example.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); }
@GetMapping("/get/{key}") public Student get(@PathVariable("key") String key){
return (Student) redisTemplate.opsForValue().get(key); } }
|
spring: redis: database: 0 host: 127.0.0.1 port: 6379
|
package com.example;
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("/{key}") public String stringTest(@PathVariable("key") String key){
redisTemplate.opsForValue().set(key, "hello world!!!"); String result = (String) redisTemplate.opsForValue().get(key); return result; }
|
@GetMapping("/list") public List<Object> listTest(){ ListOperations<String, Object> listOperations = redisTemplate.opsForList(); listOperations.leftPush("list", "hello"); listOperations.leftPush("list", "world"); listOperations.leftPush("list", 123);
List<Object> list = listOperations.range("list", 0, 2); return list; }
|
@GetMapping("/set") public Set<Object> setTest(){
SetOperations<String, Object> setOperations = redisTemplate.opsForSet(); setOperations.add("set", "hello"); setOperations.add("set", "hello"); setOperations.add("set", "world"); setOperations.add("set", "world"); setOperations.add("set", 0); setOperations.add("set", 0);
Set<Object> set = setOperations.members("set"); return set; }
|
@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> set = zSet.range("zset", 0, 2); return set; }
|
传统的HashMap是由key,value组成的,格式为:
HashMap<key, value>
而redis中的 哈希是下面格式的,它多了一个参数key:
HashOperations<key, hashkey, value>
key:每一组数据的id
hashkey和value是一组完整的 HashMap数据,通过key来区分不同的HashMap
@GetMapping("/hash") public String hashTest(){ HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash(); hashOperations.put("key", "hashKey", "hello"); String str = hashOperations.get("key", "hashKey"); return str;
}
|