是一个高性能的(key/value)分布式内存数据库,基于内存运行,并支持持久化的NoSQL数据库。
与传统数据库不同的是 Redis 的数据是存在内存中的,所以存写速度非常快,多用于缓存
Redis的基本数据结构:
五种常用类型:字符串(strings),散列(hashes),列表(lists),集合(sets),有序集合(sorted sets)
三种特殊类型:位存储(bitmaps),统计(hyperloglogs),地理空间(geospatial)
SpringBoot集成:引入依赖,添加配置类,配置连接,测试代码引入redis依赖:
org.springframework.boot
spring-boot-starter-data-redis
2.4.2
添加配置类:
package com.example.demo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")//忽略所有的警告
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(factory);
// jackson 序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// String 序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
(下边的配置类源码初学阶段了解即可,可直接跳过看下面的配置连接)
Springboot所有的配置类都会有一个自动配置类,redis也一样
配置类所在位置:
自动配置类都会绑定一个properties配置文件,提示可配置什么数据
配置连接:(application.yml语法)
spring:
redis:
#连接地址
host: 127.0.0.1
#连接密码
password: 12345
#端口
port: 6379
#Redis默认情况下有16个库,这里配置具体使用的库,默认是0
database: 4
测试代码:
package com.example.demo.controller;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/test")
//swagger注解,不用swagger可以删除
@Api(value = "后端三郎成长项目检测接口",tags = "测试--检测模块")
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping(value = "/testSetOpsForValue")
public void testSetOpsForValue(){
redisTemplate.opsForValue().set("for", "学习redis");
}
@GetMapping(value = "/testGetOpsForValue")
public String testGetOpsForValue(){
Object aFor = redisTemplate.opsForValue().get("for");
return aFor.toString();
}
}
执行testSetOpsForValue接口(RedisDesktopManager------redis连接工具查看)
执行testGetOpsForValue接口
读完欢迎您留言,有问题或者不足之处鄙人及时修正,顺便点个赞呦


