0


SpringBoot使用Redis缓存MySql

文章目录

1 项目组成

  • 应用:springboot rest api
  • 数据库:mysql
  • jdbc框架:jpa
  • 缓存中间件:redis

2 运行springboot

3 访问mysql

  • 增加gradle依赖 (通过jpa)> implementation(‘mysql:mysql-connector-java’)> implementation(‘org.springframework.boot:spring-boot-starter-data-jpa’)
  • 增加配置文件及数据库配置> 创建文件application.yml> 在这里插入图片描述spring:datasource:url: jdbc:mysql://localhost:3306/user_info username: root password: root jpa:show-sql:true
  • 类调整在这里插入图片描述在这里插入图片描述在这里插入图片描述
  • mysql insert一条数据,然后通过 http://localhost:8080/listAllUser 查询数据库

4 设置redis缓存

  • 增加gradle依赖> implementation(‘org.springframework.boot:spring-boot-starter-data-redis’)> implementation(‘org.springframework.boot:spring-boot-starter-cache’)
  • 配置文件配置redis参数spring:datasource:url: jdbc:mysql://localhost:3306/user_info username: root password: root jpa:show-sql:true## Redis 配置redis:## Redis数据库索引(默认为0)database:0## Redis服务器地址host: localhost ## Redis服务器连接端口port:6379## Redis服务器连接密码(默认为空)password:jedis:pool:## 连接池最大连接数(使用负值表示没有限制)#spring.redis.pool.max-active=8max-active:8## 连接池最大阻塞等待时间(使用负值表示没有限制)#spring.redis.pool.max-wait=-1max-wait:-1## 连接池中的最大空闲连接#spring.redis.pool.max-idle=8max-idle:8## 连接池中的最小空闲连接#spring.redis.pool.min-idle=0min-idle:0## 连接超时时间(毫秒)timeout:1200
  • Redis配置类在这里插入图片描述 RedisConfig代码packagecom.example.restservice.config;importcom.fasterxml.jackson.annotation.JsonAutoDetect;importcom.fasterxml.jackson.annotation.PropertyAccessor;importcom.fasterxml.jackson.databind.ObjectMapper;importorg.springframework.cache.CacheManager;importorg.springframework.cache.annotation.CachingConfigurerSupport;importorg.springframework.cache.annotation.EnableCaching;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.data.redis.cache.RedisCacheConfiguration;importorg.springframework.data.redis.cache.RedisCacheManager;importorg.springframework.data.redis.cache.RedisCacheWriter;importorg.springframework.data.redis.connection.RedisConnectionFactory;importorg.springframework.data.redis.core.*;importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;importorg.springframework.data.redis.serializer.StringRedisSerializer;importjava.time.Duration;/** * @author lzh * create 2019-09-24-15:07 */@Configuration@EnableCachingpublicclassRedisConfigextendsCachingConfigurerSupport{/** * 选择redis作为默认缓存工具 * @param redisConnectionFactory * @return *//*@Bean //springboot 1.xx public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); return rcm; }*/@BeanpublicCacheManagercacheManager(RedisConnectionFactory redisConnectionFactory){RedisCacheConfiguration redisCacheConfiguration =RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1));// 设置缓存有效期一小时returnRedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)).cacheDefaults(redisCacheConfiguration).build();}/** * retemplate相关配置 * @param factory * @return */@BeanpublicRedisTemplate<String,Object>redisTemplate(RedisConnectionFactory factory){RedisTemplate<String,Object> template =newRedisTemplate<>();// 配置连接工厂 template.setConnectionFactory(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)Jackson2JsonRedisSerializer jacksonSeial =newJackson2JsonRedisSerializer(Object.class);ObjectMapper om =newObjectMapper();// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om);// 值采用json序列化 template.setValueSerializer(jacksonSeial);//使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(newStringRedisSerializer());// 设置hash key 和value序列化模式 template.setHashKeySerializer(newStringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet();return template;}/** * 对hash类型的数据操作 * * @param redisTemplate * @return */@BeanpublicHashOperations<String,String,Object>hashOperations(RedisTemplate<String,Object> redisTemplate){return redisTemplate.opsForHash();}/** * 对redis字符串类型数据操作 * * @param redisTemplate * @return */@BeanpublicValueOperations<String,Object>valueOperations(RedisTemplate<String,Object> redisTemplate){return redisTemplate.opsForValue();}/** * 对链表类型的数据操作 * * @param redisTemplate * @return */@BeanpublicListOperations<String,Object>listOperations(RedisTemplate<String,Object> redisTemplate){return redisTemplate.opsForList();}/** * 对无序集合类型的数据操作 * * @param redisTemplate * @return */@BeanpublicSetOperations<String,Object>setOperations(RedisTemplate<String,Object> redisTemplate){return redisTemplate.opsForSet();}/** * 对有序集合类型的数据操作 * * @param redisTemplate * @return */@BeanpublicZSetOperations<String,Object>zSetOperations(RedisTemplate<String,Object> redisTemplate){return redisTemplate.opsForZSet();}}
  • 代码通过@Cacheable使用 redis缓存在这里插入图片描述
  • 访问接口后,通过redis工具查询数据> 点击 redis-lic.exe> 命令 keys * 在这里插入图片描述

本文转载自: https://blog.csdn.net/lanxing_huangyao/article/details/123042333
版权归原作者 lanxing_thk 所有, 如有侵权,请联系我们删除。

“SpringBoot使用Redis缓存MySql”的评论:

还没有评论