redis的安装和springboot整合redis
程序员文章站
2022-04-30 17:17:54
...
Redis的安装,以及SpringBoot整合Redis实现缓存操作
一、Redis的安装
1、使用docker拉取redis镜像
docker pull redis
2、制作redis的容器
docker run --name -p 6379:6379 -d redis
3、查看redis容器是否启动
docker ps -a
---如果没有启动就启动redis
docker start 容器ID
二、SpringBoot整合Redis实现缓存
1、配置yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
redis:
host: 192.168.159.135
port: 6379
password:
database: 0
logging:
level:
com.changan.springboot_redis.mapper.*: debug
debug: true
mybatis:
type-aliases-package: com.tdh.studentmgr.pojo
mapper-locations: classpath:mapper/*.xml
2、在service层的实现类中进行逻辑处理
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public List<Student> findAll() {
String key="Student";//设置键名
ListOperations<String,Student> list = redisTemplate.opsForList();
Boolean bool=redisTemplate.hasKey(key);//判断是否存在该键名
if (bool){
return list.range(key,0,-1);//如果存在直接从缓存查询返回集合
}else {
List<Student> students = studentMapper.findAll();
System.out.println(students);
list.leftPushAll(key,students);//如果不存在将设置好的key键值和查询数据库的结果放入
return students ;//既然都查出来了就直接返回咯
}
}
@Override
public Student findById(Integer id) {
String key = "student_"+id;
//先去redis数据库中查询有没有对应的key值
Boolean hasKey = redisTemplate.hasKey(key);
//得到操作string类型的redis对象
ValueOperations<String,Student> operations = redisTemplate.opsForValue();
if(hasKey){//如果有
//通过key获取value返回给服务器
Student student = operations.get(key);
return student;
}
Student byId = studentMapper.findById(id);
operations.set(key,byId);
return byId;
}
3、序列化设置 RedisTemplateConfig
package com.changan.springboot_redis.config;
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 java.net.UnknownHostException;
@Configuration
public class RedisTemplateConfig {
//覆盖默认的自动配置
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//修改默认的序列化规则
//1.创建序列化规则对象
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
//2.更改默认的序列化规则
template.setDefaultSerializer(jackson2JsonRedisSerializer);
return template;
}
}
4、控制层(不做逻辑处理,直接调用就好!)
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students")
public List<Student> findAll(){
return studentService.findAll();
}
@GetMapping("/student/{id}")
public Student findById(@PathVariable("id")Integer id){
return studentService.findById(id);
}
}
上一篇: shell find命令使用简介
推荐阅读
-
scrapy-redis的安装部署步骤讲解
-
Redis系列(一):Redis的简介与安装
-
Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
-
JAVAEE——宜立方商城06:Redis安装、数据类型和持久化方案、Redis集群分析与搭建、实现缓存和同步
-
SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]
-
SpringBoot 2.x 开发案例之 Shiro 整合 Redis
-
Windows 64位下安装Redis和可视化工具Redis Desktop Manager的图文教程
-
Redis的使用--基本数据类型的操作命令和应用场景
-
Springboot整合redis步骤
-
centos7安装php7.3的redis扩展(不是redis服务!)