springboot整合redis以及springboot使用redis作为缓存

springboot整合redis以及springboot使用redis作为缓存

1.java连接redis

创建一个简单的maven项目
在pom.xml文件钟引入jedis依赖

<dependencies> 
	<dependency> 
		<groupId>redis.clientsgroupId> 
		<artifactId>jedisartifactId> 
		<version>3.6.0version> 
	dependency> 
dependencies>

java代码操作redis

首先要确保虚拟机中redis开启,并且配置文件redis.conf文件中连接为bind 0.0.0.0,端口号为6379,并且防火墙关闭,还有清除干净redis库中的数据方便操作。
springboot整合redis以及springboot使用redis作为缓存_第1张图片
先在数据库中放入(set)key值为k1,value值为v1的数据,然后读取(get)key值为k1的数据打印输出,然后再存入(hset)hash类型的数据,此时的name和text就会当作field字段存入数据库中,获取key中所有的field的value值可以用hgetAll,flushAll是将数据库中所有数据库数据信息清除,mset和mget是存入和读取多个key,value。
测试结果
springboot整合redis以及springboot使用redis作为缓存_第2张图片
虚拟机中可以看到只剩下两个key值
springboot整合redis以及springboot使用redis作为缓存_第3张图片

2.springboot整合redis

引入redis的启动依赖------>spring-boot-starter-data-redis

<dependency>
	<groupId>org.springframework.bootgroupId> 
	<artifactId>spring-boot-starter-data-redisartifactId> 
dependency>


<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
dependency>

然后是连接redis数据库,在application.properties配置文件里
在这里插入图片描述
springboot整合redis以及springboot使用redis作为缓存_第4张图片

这里要把刚刚操作的数据都清楚干净flushall,然后在单元测试中测试
在这里插入图片描述
操作StringRedisTemplate

1操作String类型的
springboot整合redis以及springboot使用redis作为缓存_第5张图片

测试成功
在这里插入图片描述

2.1操作Hash类型的

springboot整合redis以及springboot使用redis作为缓存_第6张图片

springboot整合redis以及springboot使用redis作为缓存_第7张图片

2.2操作RedisTemplate

它的key和value可以存放Object类型,那么它的key和value默认采用的序列化方式为JDK序列化方式。一般我们需要
为key和value指定序列化方式。

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());

springboot整合redis以及springboot使用redis作为缓存_第8张图片
springboot整合redis以及springboot使用redis作为缓存_第9张图片
redis可视化工具结果没有乱码
在这里插入图片描述
在这里插入图片描述

2.3连接集群

哨兵模式
application.properties配置文件里写

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.71.3:26379

修改哨兵配置文件sentinel.conf
在这里插入图片描述
springboot整合redis以及springboot使用redis作为缓存_第10张图片
springboot整合redis以及springboot使用redis作为缓存_第11张图片

springboot整合redis以及springboot使用redis作为缓存_第12张图片
springboot整合redis以及springboot使用redis作为缓存_第13张图片
springboot整合redis以及springboot使用redis作为缓存_第14张图片
测试
springboot整合redis以及springboot使用redis作为缓存_第15张图片
去中心化模式

在这里插入图片描述
springboot整合redis以及springboot使用redis作为缓存_第16张图片
测试
springboot整合redis以及springboot使用redis作为缓存_第17张图片
springboot整合redis以及springboot使用redis作为缓存_第18张图片

3.springboot使用redis作为缓存

作用: 为了减少对数据库的访问频率。从而提高项目的性能。
springboot整合redis以及springboot使用redis作为缓存_第19张图片
什么样的数据适合放入缓存中。

1. 查询频率高 
2. 修改频率低 
3. 数据安全行要求低的

创建简单的springboot工程
pom.xml引入相关依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.4.3version>
        dependency>
    dependencies>

application.properties配置文件内容
springboot整合redis以及springboot使用redis作为缓存_第20张图片
实体
springboot整合redis以及springboot使用redis作为缓存_第21张图片

mapper
springboot整合redis以及springboot使用redis作为缓存_第22张图片
service
springboot整合redis以及springboot使用redis作为缓存_第23张图片
controller
springboot整合redis以及springboot使用redis作为缓存_第24张图片
主启动类一定要加MapperScan注解
springboot整合redis以及springboot使用redis作为缓存_第25张图片
开启redis哨兵模式
springboot整合redis以及springboot使用redis作为缓存_第26张图片

启动测试
第一次查询useid为1的数据
springboot整合redis以及springboot使用redis作为缓存_第27张图片
在这里插入图片描述
清除控制台
再访问一次
springboot整合redis以及springboot使用redis作为缓存_第28张图片
springboot整合redis以及springboot使用redis作为缓存_第29张图片
没有走数据库
redis缓存中已经有了这条数据
在这里插入图片描述
测试成功!!!!

你可能感兴趣的:(springboot整合redis以及springboot使用redis作为缓存)