1. SpringBoot整合Redis1.1 安装Redis并启动123456789101112131415161718192021#docker安装curl -sSL https://get.daocloud.io/docker | sh#拉取docker pull redis:latest#创建容器并启动docker run -p 6379:6379 --name redis -v /usr/local/docker/redis/redis.conf:/opt/docker/redis/redis.conf -v /usr/local/docker/redis/data:/opt/docker/redis/data -d redis redis-server /opt/docker/redis/redis.conf --appendonly yes#-p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。# --name redis 指定该容器名称,查看和进行操作都比较方便。# -v 挂载目录,规则与端口映射相同。# -d redis 表示后台启动redis# redis-server /opt/docker/redis/redis.conf #以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录/usr/local/docker/redis/redis.confdocker start redis#进入容器docker exec -it redis /bin/bash#验证redis-cli 1.2 SpringBoot添加Redis的配置并导入依赖1234<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 123## redis配置spring.redis.port=6379spring.redis.host=192.168.200.100 1.3 使用123456789<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> 对之前的findAll代码 做一个缓存 1234567891011121314151617@Autowired private RedisTemplate<String,String> redisTemplate; public List<User> findAll(){ String userListJsonStr = redisTemplate.opsForValue().get("UserService.findAll"); if (StringUtils.isNotBlank(userListJsonStr)){ List<User> users = JSON.parseArray(userListJsonStr, User.class); log.info("走了缓存~~~"); return users; }else { //查询所有 List<User> users = userMapper.selectList(new LambdaQueryWrapper<>()); redisTemplate.opsForValue().set("UserService.findAll",JSON.toJSONString(users),2, TimeUnit.HOURS); log.info("存入缓存~~~"); return users; } }