0004SpringBoot整合Redis
2024-10-06 09:38:49
在已经整合了SpringDataJPA和Junit的基础上,整合Redis,只需要一下几步即可:
1、下载64windows版的Redis安装包、解压并启动服务端
2、配置Redis的起步依赖(pom.xml)
3、配置连接Redis服务器的信息(application.propertis)
4、写测试类
5、启动测试
具体内容如下:
下载64windows版的Redis安装包、解压并启动服务端
下载地址为:
https://github.com/MicrosoftArchive/redis/releases
下载64位的版本,如下图:
解压后:
双击redis-server.exe即可启动redis服务端
双击redis-cli.exe即可启动redis客户端
pom.xml中配置Redis的起步依赖:
<!--redis的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties中配置连接redis服务器的信息:
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
编写测试类:
package com.myself; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myself.domain.User;
import com.myself.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaApplication.class)
public class RedisTest {
@Autowired
private RedisTemplate<String,String> redisTemplate; @Autowired
private UserRepository userRepository; @Test
public void queryUsers() throws JsonProcessingException {
//从redis缓存中查询数据,redis是nosql的K-V键值对,所以设置键为user.findAll
String listUserJson = redisTemplate.boundValueOps("user.findAll").get();
//如果数据为空,则为首次访问,需要从数据库中查询数据
if(listUserJson == null){
//从数据库中查询数据
List<User> users = userRepository.findAll();
//将数据转换为json字符串,由于我们配置了web的起步依赖,所以我们可以使用Jackson进行数据转换
//jackson中的对象
ObjectMapper objectMapper = new ObjectMapper();
listUserJson = objectMapper.writeValueAsString(users);
redisTemplate.boundValueOps("user.findAll").set(listUserJson);
System.out.println("=================从数据库中查询数据=============================================");
}else{
System.out.println("=================从redis缓存中查询数据=============================================");
}
System.out.println("user数据为:" + listUserJson); } } 如有理解不到位之处,望指正。 除了以上方式指定将数据存入Redis,还可以指定开启Redis缓存
1、引导类上添加@EnableCaching
package com.myself; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication
//开启Redis缓存
@EnableCaching
public class SpringbootJpaApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootJpaApplication.class, args);
}
}
2、在UserController.java的查询方法上加上@Cacheable(value = "queryUserList")
package com.myself.controller; import com.myself.domain.User;
import com.myself.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
public class UserController {
@Autowired
private UserRepository userRepository; @RequestMapping("/queryUserList")
@ResponseBody
@Cacheable(value = "queryUserList")
public String queryUserList(){
System.out.println("===========打印此信息,说明没有走Redis缓存==========");
// List<User> users = userRepository.findAll();
return "abc";
} } 通过以上2步,就可以开启Redis缓存,并指定在缓存中对应的key,通过页面首次访问时不走缓存,控制台打印信息,
再次访问时走Redis缓存,控制台不会再打印信息,也可通过Redis查看是否生成对应的key
最新文章
- mysql每秒最多能插入多少条数据 ? 死磕性能压测
- SQL语句大全(转载)
- Dr.com──加密方式(网页端)
- Java设计模式-装饰模式(Decorator)
- Java基础之创建窗口——使用网格布局管理器(TryGridLayout)
- 使用 Filter 完成一个简单的权限模型
- Unity中对象池的使用
- android 5.0新特性CardView教程
- 【源代码】基于Android和蓝牙的单片机温度採集系统
- python每天进步一点点
- 冲刺NO.11
- 利用layui前端框架实现对不同文件夹的多文件上传
- Python网络数据采集PDF
- Linux下对inode和块的理解
- Docker学习笔记之搭建Docker运行环境
- SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现
- MongoDB numa系列问题二:WARNING: You are running on a NUMA machine.
- spoj MINSUB 单调栈+二分
- AssetBundle中Unload()方法的作用
- hdu 1397 (素数判定)
热门文章
- el-table中通过renderHeader方法为表头添加hover等效果
- cent8安装postgres
- 深度图转伪彩色图(python)
- [转帖]PG语法解剖--基本sql语句用法入门
- AlgorithmMap Dev Log
- WUST 设计模式 实验一 单例模式的应用
- deferred.promise.then().then()异步链式操作(Chain operation)
- C#委托和事件的使用示例
- (八)springmvc之静态资源的访问。
- Java HeapSort