文章目录

  • Springboot:整合redis对接口数据进行缓存
    • 一、注解及其属性介绍
      • @Cacheable
      • @CacheEvict
      • @CachePut
      • @CacheConfig
    • 二、代码实现
      • 1.创建数据库
      • 2.application.yaml配置问加你
      • 3.redisConfig.java配置类
      • 4.UserInfo实体类
      • 5.UserMapper.java
      • 6.Uservice及实现类
      • 7.UserController.java
      • ==8.注意==
    • 三、测试
      • 1.测试查询
      • 2.测试新增
      • 3.测试修改
      • 4.测试删除

源码地址:https://gitee.com/huanglei1111/springboot-demo/tree/master/springboot-ehcache

Springboot:整合redis对接口数据进行缓存

一、注解及其属性介绍

@Cacheable

  • cacheNames:指定缓存的名称

@Cacheable({"menu", "menuById"})关联多个缓存名,当执行方法之前,这些关联的每一个缓存都会被检查,而且只要至少其中一个缓存命中了,那么这个缓存中的值就会被返回

  • key:定义组成的key值,如果不定义,则使用全部的参数计算一个key值。可以使用spring El表达式

  • keyGenerator:定义key生成的类,和key的不能同时存在

  • sync:如果设置sync=true:a. 如果缓存中没有数据,多个线程同时访问这个方法,则只有一个方法会执行到方法,其它方法需要等待; b. 如果缓存中已经有数据,则多个线程可以同时从缓存中获取数据

  • condition和unless 只满足特定条件才进行缓存:

    1.condition: 在执行方法前,condition的值为true,则缓存数据

    2.unless :在执行方法后,判断unless ,如果值为true,则不缓存数据

    3.conditon和unless可以同时使用,则此时只缓存同时满足两者的记录

@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

@CacheEvict

删除缓存

  • allEntries = true: 清空缓存book1里的所有值
  • allEntries = false: 默认值,此时只删除key对应的值

@CachePut

每次执行都会执行方法,无论缓存里是否有值,同时使用新的返回值的替换缓存中的值。这里不同@Cacheable:@Cacheable如果缓存没有值,从则执行方法并缓存数据,如果缓存有值,则从缓存中获取值

@CacheConfig

@CacheConfig: 类级别的注解:如果我们在此注解中定义cacheNames,则此类中的所有方法上 @Cacheable的cacheNames默认都是此值。当然@Cacheable也可以重定义cacheNames的值

二、代码实现

1.创建数据库

-- 判断数据表是否存在,存在则删除
DROP TABLE IF EXISTS tb_user;-- 创建“用户信息”数据表
CREATE TABLE `tb_user` (`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',`user_name` varchar(50) NOT NULL COMMENT '用户名称',`blog_url` varchar(50) NOT NULL COMMENT '博客地址',`blog_remark` varchar(50) DEFAULT NULL COMMENT '博客备注',PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户信息表';-- 添加数据
INSERT INTO tb_user(user_name,blog_url,blog_remark) VALUES('拒绝熬夜啊的博客','https://blog.csdn.net/weixin_43296313/','您好,欢迎访问 拒绝熬夜啊的博客');

2.application.yaml配置问加你

#Spring配置
spring:#缓存管理器cache:type: redis#Redis配置redis:database: 0 #Redis数据库索引(默认为0)host: 127.0.0.1 #Redis服务器地址port: 6379 #Redis服务器连接端口password:  #Redis服务器连接密码(默认为空)jedis:pool:max-active: 8 #连接池最大连接数(使用负值表示没有限制)max-wait: -1s #连接池最大阻塞等待时间(使用负值表示没有限制)max-idle: 8  #连接池中的最大空闲连接min-idle: 0 #连接池中的最小空闲连接lettuce:shutdown-timeout: 100ms #关闭超时时间,默认值100ms#使用Thymeleaf模板引擎thymeleaf:mode: HTML5encoding: UTF-8cache: false  #使用Thymeleaf模板引擎,关闭缓存servlet:content-type: text/html#DataSource数据源datasource:url: jdbc:mysql://localhost:3306/mybatis_test?useSSL=false&ampusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver#MyBatis配置
mybatis:type-aliases-package: com.hl.springbootehcache.pojo #别名定义configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #指定 MyBatis 所用日志的具体实现,未指定时将自动查找map-underscore-to-camel-case: true #开启自动驼峰命名规则(camel case)映射lazy-loading-enabled: true #开启延时加载开关aggressive-lazy-loading: false #将积极加载改为消极加载(即按需加载),默认值就是false#lazy-load-trigger-methods: "" #阻挡不相干的操作触发,实现懒加载cache-enabled: true #打开全局缓存开关(二级环境),默认值就是true

3.redisConfig.java配置类

package com.hl.springbootehcache.config;import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;import java.lang.reflect.Method;
import java.time.Duration;@Configuration
public class RedisConfig extends CachingConfigurerSupport {//配置redis的过期时间private static final Duration TIME_TO_LIVE = Duration.ZERO;@Bean(name = "jedisPoolConfig")public JedisPoolConfig jedisPoolConfig() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();//控制一个pool可分配多少个jedis实例jedisPoolConfig.setMaxTotal(500);//最大空闲数jedisPoolConfig.setMaxIdle(200);//每次释放连接的最大数目,默认是3jedisPoolConfig.setNumTestsPerEvictionRun(1024);//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);//连接的最小空闲时间 默认1800000毫秒(30分钟)jedisPoolConfig.setMinEvictableIdleTimeMillis(-1);jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);//最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。jedisPoolConfig.setMaxWaitMillis(1500);jedisPoolConfig.setTestOnBorrow(true);jedisPoolConfig.setTestWhileIdle(true);jedisPoolConfig.setTestOnReturn(false);jedisPoolConfig.setJmxEnabled(true);jedisPoolConfig.setBlockWhenExhausted(false);return jedisPoolConfig;}@Bean("connectionFactory")public JedisConnectionFactory connectionFactory() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();redisStandaloneConfiguration.setHostName("127.0.0.1");redisStandaloneConfiguration.setDatabase(0);
//        redisStandaloneConfiguration.setPassword(RedisPassword.of("123456"));redisStandaloneConfiguration.setPort(6379);//获得默认的连接池构造器JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();//指定jedisPoolConifig来修改默认的连接池构造器(真麻烦,滥用设计模式!)jpcb.poolConfig(jedisPoolConfig());//通过构造器来构造jedis客户端配置JedisClientConfiguration jedisClientConfiguration = jpcb.build();//单机配置 + 客户端配置 = jedis连接工厂return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);}@Beanpublic RedisTemplate<Object, Object> redisTemplate(JedisConnectionFactory connectionFactory) {RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(connectionFactory);GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);//初始化参数和初始化工作redisTemplate.afterPropertiesSet();return redisTemplate;}/*** 缓存对象集合中,缓存是以key-value形式保存的,* 当不指定缓存的key时,SpringBoot会使用keyGenerator生成Key。*/@Override@Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object target, Method method, Object... params) {StringBuilder sb = new StringBuilder();//类名+方法名sb.append(target.getClass().getName());sb.append(method.getName());for (Object obj : params) {sb.append(obj.toString());}return sb.toString();}};}/*** 缓存管理器* @param connectionFactory 连接工厂* @return  cacheManager*/@Beanpublic CacheManager cacheManager(JedisConnectionFactory connectionFactory) {//新建一个Jackson2JsonRedis的redis存储的序列化方式GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();//解决查询缓存转换异常的问题// 配置序列化(解决乱码的问题)//entryTtl设置过期时间//serializeValuesWith设置redis存储的序列化方式RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(TIME_TO_LIVE).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)).disableCachingNullValues();//定义要返回的redis缓存管理对象return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).build();}
}

4.UserInfo实体类

@Data
public class UserInfo implements Serializable {private int userId; //用户编号private String userName; //用户姓名private String blogUrl; //博客地址private String blogRemark; //博客信息
}

5.UserMapper.java

@Mapper
@Repository
public interface UserMapper {/*** 根据用户ID,获取用户信息*/@Select("SELECT * FROM tb_user WHERE user_id = #{userId}")public UserInfo getUserById(int userId);/*** 新增用户,并获取自增主键*/@Insert("INSERT INTO tb_user(user_name,blog_url,blog_remark) VALUES(#{userName},#{blogUrl},#{blogRemark});")@Options(useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userId")public int insertUser(UserInfo userInfo);/*** 修改用户*/@Update("UPDATE tb_user SET user_name = #{userName},blog_url = #{blogUrl} ,blog_remark = #{blogRemark} WHERE user_id = #{userId}")public int updateUser(UserInfo userInfo);/*** 删除用户*/@Delete("DELETE FROM tb_user WHERE user_id = #{userId}")public int deleteUser(int userId);
}

6.Uservice及实现类

public interface UserService {UserInfo getUserById(int userId);UserInfo insertUser(UserInfo userInfo);UserInfo updateUser(UserInfo userInfo);int deleteUser(int userId);
}/*** 用户信息业务逻辑类* @author hl* 缓存就在这层工作* @Cacheable,将查询结果缓存到redis中,(key="#p0")指定传入的第一个参数作为redis的key。* @CachePut,指定key,将更新的结果同步到redis中* @CacheEvict,指定key,删除缓存数据,allEntries=true,方法调用后将立即清除缓存**/
@CacheConfig(cacheNames = "userCache")
@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;/*** 根据用户ID,获取用户信息*/@Override@Cacheable(key = "#p0") // #p0 表示第一个参数public UserInfo getUserById(int userId) {return userMapper.getUserById(userId);}/*** 新增用户,并获取自增主键*/@Override@CachePut(key = "#p0.userId")public UserInfo insertUser(UserInfo userInfo) {userMapper.insertUser(userInfo);return userInfo;}/*** 修改用户*/@Override@CachePut(key = "#p0.userId")public UserInfo updateUser(UserInfo userInfo) {userMapper.updateUser(userInfo);return userInfo;}/*** 删除用户* 如果在@CacheEvict注解中添加allEntries=true属性,* 将会删除所有的缓存*/@Override@CacheEvict(key = "#p0")public int deleteUser(int userId) {return userMapper.deleteUser(userId);}
}

7.UserController.java

@Controller
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 获取用户信息*/@RequestMapping(value = "getUserById",method = RequestMethod.GET)@ResponseBodypublic HttpResponseTemp<?> getUserById(int userId) {UserInfo userInfo = userService.getUserById(userId);return ResultStat.OK.wrap(userInfo,"获取用户信息成功");}/*** 新增用户*/@ResponseBody@RequestMapping("insertUser")public HttpResponseTemp<?> insertUser() {//创建新用户UserInfo userInfo = new UserInfo();userInfo.setUserName("hl的博客");userInfo.setBlogUrl("www.baidu.com");userInfo.setBlogRemark("您好,欢迎访问 hl的博客");UserInfo userInfo1 = userService.insertUser(userInfo);return ResultStat.OK.wrap(userInfo1.getUserId(),"新增用户成功");}/*** 修改用户*/@ResponseBody@RequestMapping("updateUser")public HttpResponseTemp<?> updateUser(int userId) {UserInfo userInfo = new UserInfo();userInfo.setUserId(userId);userInfo.setUserName("hl的博客_02");userInfo.setBlogUrl("www.baidu.com");userInfo.setBlogRemark("您好,欢迎访问 hl的博客");UserInfo userInfo1 = userService.updateUser(userInfo);return ResultStat.OK.wrap(userInfo1.getUserId(),"修改用户成功");}/*** 删除用户*/@ResponseBody@RequestMapping("deleteUser")public HttpResponseTemp<?> deleteUser(int userId) {int result = userService.deleteUser(userId);return ResultStat.OK.wrap(result,"删除用户成功");}
}

8.注意

  • 实体类要实现序列化 public class UserInfo implements Serializable
  • 在项目启动类加上注解@EnableCaching开启缓存

三、测试

1.测试查询

redis中也有数据了

查看控制台的sql语句,只有一条打印,说明是只有第一条是从数据库中查询的其他的是从redis中获取的

2.测试新增

3.测试修改

这里博客名称变成02了

4.测试删除

Springboot:整合redis对接口数据进行缓存相关推荐

  1. SpringBoot整合Redis配置MyBatis二级缓存

    目录 写在前面 源码获取 一.MyBatis缓存机制 1.1.一级缓存 1.2.二级缓存 二.集成Redis 2.1.安装Redis 2.2.项目引入Redis 2.2.1.Maven依赖 2.2.2 ...

  2. spring-boot 整合redis作为数据缓存

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  3. springboot整合redis分别实现手动缓存和注解缓存

    一.前期准备 一个构建好的springboot系统 下载redis安装包,去redis官网下载 启动redis服务,windows下双击bin目录下的redis-service.exe 二.环境构建 ...

  4. springboot整合redis做缓存

    之前的项目中,用到过redis,主要是使用redis做缓存,redis在web开发中使用的场景很多,其中缓存是其中一个很重要的使用场景,之所以用作缓存,得益于redis的读写数据,尤其是在读取数据的时 ...

  5. Springboot整合redis实现缓存及其缓存运行原理浅析

    声明:小白,学习阶段,主要目的是为了记录学习过程,本文仅供参考,如有不足的地方欢迎指出讨论交流 本文基于Springboot2.1.3版本开发: 准备阶段 首先是pom.xml文件所需的依赖: < ...

  6. SpringBoot整合Redis缓存

    SpringBoot整合Redis缓存 一.缓存概念知识 1.是什么缓存 2.缓存的优缺点 3.为什么使用缓存 二.Redis概念知识 1.Redis简介 2.为什么用Redis作为缓存 3.Redi ...

  7. 8分钟带你学会SpringBoot整合Redis来实现缓存技术

    1.概述 随着互联网技术的发展,对技术要求也越来越高,所以在当期情况下项目的开发中对数据访问的效率也有了很高的要求,所以在项目开发中缓存技术使用的也越来越多,因为它可以极大的提高系统的访问速度,关于缓 ...

  8. SpringBoot整合Redis+Redis缓存应用+Redis实现Session共享+...

    一.SpringBoot整合Redis 1.导入依赖 <!--存在Redis依赖--> <dependency><groupId>org.springframewo ...

  9. Springboot整合redis(lettuce)

    springboot 整合redis(lettuce) 首先确保电脑上装了redis.最好能用redisDesktop查看一下数据情况 redis是一款非常流行的Nosql数据库.redis的功能非常 ...

最新文章

  1. 2021年大数据ELK(十六):Elasticsearch SQL(职位查询案例)
  2. 最近面试Java后端开发的感受
  3. 【数据结构作业心得】纸面6 - Matlab LU分解
  4. 调整cisco参数设置
  5. Leetcode 130. 被围绕的区域 (每日一题 20210720 同类型题)
  6. 【温故知新】CSS学习笔记(显示模式简介)
  7. 2008 年 十大预测的总结 (只对了一半左右)
  8. java控制台打印图片_java——控制台输入打印图形
  9. php实现mysql 数据迁移_php实现redis数据库指定库号迁移的方法
  10. Python 扩展知识:编程习惯
  11. [职场]白领常见的18个职场困惑
  12. 安装mysql8避坑指南_2019 MySQL 8 安全安装避坑指南-Go语言中文社区
  13. 华为云怎么修改服务器密码,云服务器登陆密码如何修改
  14. 概率密度函数曲线及绘制
  15. mysql 截取第一个字符_MySQL 字符串截取SUBSTRING()函数
  16. 一种相对高效的按键消抖方法
  17. Unity UGUI图文混排源码--优化版
  18. xiangmu--WAD
  19. 实验吧-杂项-MD5之守株待兔(时间戳python时间戳函数time.time())
  20. 实战Kubernetes动态卷存储(NFS)

热门文章

  1. 剑桥商务英语中级 BEC
  2. mytunet on DD-Wrt on La Fonera
  3. 目前华为手机有哪几款是鸿蒙系统,可升级鸿蒙系统机型曝光 华为手机优先 看看有你的手机吗?...
  4. 2020年鸿蒙手机在千元机吗,鸿蒙OS升级计划表曝光,华为千元机也有份!
  5. 装逼利器 25个Python小技巧骚断同事腰
  6. python部落之python刷题宝之基础知识
  7. 披荆斩棘、共赴未来-2020总结及2021年计划
  8. 小程序数据请求注意事项
  9. 2020-12-29js基本知识
  10. Drozer自定义模块以及安装使用