Redis是一个基于内存的key-value结构数据库。Redis 是互联网技术领域使用最为广泛的存储中间件。
官网:https://redis.io
中文网:https://www.redis.net.cn/
在安装目录下使用以下命令
redis-server.exe redis.windows.conf
默认端口号6379,使用ctrl+c结束服务
启动后可使用客户端连接
redis-dli.exe
默认连接本地Redis服务,6379端口
可通过-h指定ip地址,-p指定端口,-a指定密码
通过修改redis.windows.conf文件设置redis连接密码
requirepass 123456
Redis存储的是key-value结构的数据,key是字符串类型,value有五种常见的数据类型
Redis 中字符串类型常用命令:
更多命令可以参考Redis中文网:https://www.redis.net.cn
Redis hash 是一个string类型的 field 和 value 的映射表,hash特别适合用于存储对象,常用命令:
Redis 列表是简单的字符串列表,按照插入顺序排序,常用命令:
Redis set 是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据,常用命令:
Redis有序集合是string类型元素的集合,且不允许有重复成员。每个元素都会关联一个double类型的分数。常用命令:
常用命令:
Redis的通用命令是不分数据类型的,都可以使用的命令:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
sky:
redis:
host: localhost
port: 6379
password: 123456
database: 10
解释说明:
database:指定使用Redis的哪个数据库,Redis服务启动后默认有16个数据库,编号分别是从0到15。
可以通过修改Redis配置文件来指定数据库的数量。
package com.sky.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* 当前配置类并非必须的,SpringBoot会自动装配RedisTemplate对象,但是默认的key序列化器为jdkSerializationRedisSerializer,
* 导致存入redis的数据与原始数据有区别,故设置为StringRedisSerializer序列化器
*/
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板对象...");
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
package com.sky;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class SpringDataRedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedisTemplate() {
System.out.println(redisTemplate);//org.springframework.data.redis.core.RedisTemplate@2ef3efcc
//String类型数据操作
ValueOperations valueOperations = redisTemplate.opsForValue();
//Hash类型数据操作
HashOperations hashOperations = redisTemplate.opsForHash();
//List类型数据操作
ListOperations listOperations = redisTemplate.opsForList();
//Set类型数据操作
SetOperations setOperations = redisTemplate.opsForSet();
//Zset类型数据操作
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
}
/**
* 操作字符串类型的数据
* set 设置指定key的值
* get 获取指定key的值
* setex 设置指定key的过期时间为指定值
* setnx 不存在某个key时才设置改key的值
*/
@Test
public void testString() {
// set get setex setnx
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("name", "时帆");
String name = (String) valueOperations.get("name");
System.out.println(name);
valueOperations.set("age", 18, 10, TimeUnit.SECONDS);
valueOperations.setIfAbsent("gender", "boy");
valueOperations.setIfAbsent("gender", "girl");
valueOperations.setIfPresent("hobby", "sport");
}
@Test
public void testString1() {
ValueOperations valueOperations = redisTemplate.opsForValue();
System.out.println(valueOperations.get("age"));//null,已过期,10s
System.out.println(valueOperations.get("gender"));//boy
System.out.println(valueOperations.get("hobby"));//null
}
/**
* 操作Hash数据类型
* hset
* hget
* hdel
* hkeys
* hvals 见名知意
*/
@Test
public void testHash() {
//获取操作对象
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("3", "name", "shifan");
hashOperations.put("3", "age", "18");
String name = (String) hashOperations.get("3", "name");
System.out.println(name);//shifan
Set keys = hashOperations.keys("3");
System.out.println(keys);//[name, age]
List values = hashOperations.values("3");
System.out.println(values);//[shifan, 18]
hashOperations.delete("3", "age");
System.out.println(hashOperations.get("3", "age"));//null
}
/**
* 操作List(列表)数据类型
*/
@Test
public void testList() {
//lpush,lrange,rpop,llen
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("myList","a","b","c");
listOperations.leftPush("myList","d");
List myList = listOperations.range("myList", 0, -1);
System.out.println(myList);//[d, c, b, a]
System.out.println(listOperations.rightPop("myList"));//a
System.out.println(listOperations.size("myList"));//3
}
/**
* 操作集合(Set)类型数据
*/
@Test
public void testSet(){
//sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c");
setOperations.add("set2","a","b","1");
Set set1 = setOperations.members("set1");
System.out.println(set1);//[b, a, c]
Long set2 = setOperations.size("set2");
System.out.println(set2);//3
Set intersect = setOperations.intersect("set1", "set2");
System.out.println(intersect);//[b, a]
Set union = setOperations.union("set1", "set2");
System.out.println(union);//[a, b, 1, c]
setOperations.remove("set1","a","b");
System.out.println(setOperations.members("set1"));//[c]
}
/**
* 操作有序集合类型
*/
@Test
public void testZset(){
//zadd zrange zincrby zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("myZset","a",3);
zSetOperations.add("myZset","b",4);
zSetOperations.add("myZset","c",2);
Set myZset = zSetOperations.range("myZset", 0, -1);
System.out.println(myZset);//[c, a, b]
zSetOperations.incrementScore("myZset","a",3);
System.out.println(zSetOperations.range("myZset", 0, -1));//[c, b, a]
zSetOperations.remove("myZset","b");
System.out.println(zSetOperations.range("myZset", 0, -1));//[c, a]
}
/**
* 通用命令操作
*/
@Test
public void testCommon(){
Set keys = redisTemplate.keys("*");
System.out.println(keys);//[gender, set1, set2, name, 3, myZset, myList]
System.out.println(redisTemplate.hasKey("name"));//true
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());//STRING,SET,SET,STRING,HASH,ZSET,LIST
}
redisTemplate.delete("myList");
}
}
将管理端和用户端的接口文档分开展示
在配置类中做如下修改
/**
* 通过knife4j生成管理端接口文档
* @return
*/
@Bean
public Docket docketAdmin() {
log.info("准备生成接口文档...");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("苍项目接口文档")
.version("2.0")
.description("项目接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("管理端接口")
.apiInfo(apiInfo)
.select()
//指定生成接口需要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin"))
.paths(PathSelectors.any())
.build();
return docket;
}
/**
* 通过knife4j生成用户端接口文档
* @return
*/
@Bean
public Docket docketUser() {
log.info("准备生成接口文档...");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("项目接口文档")
.version("2.0")
.description("项目接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("用户端接口")
.apiInfo(apiInfo)
.select()
//指定生成接口需要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.sky.controller.user"))
.paths(PathSelectors.any())
.build();
return docket;
}