1、创建maven项目

2、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>springWithRedis</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin></plugins></build><dependencies><!-- lombok依赖 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.18</version></dependency><!-- spring集成redis依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.2.8.RELEASE</version></dependency><!-- spring依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId><version>2.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.2.8.RELEASE</version><scope>test</scope></dependency></dependencies>
</project>

2、application.properties 配置文件

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=**********#配置缓存相关
cache.default.expire-time=20000
cache.user.expire-time=1800
cache.user.name=suiyiqi

3、实体类

package com.cache.redis.entity;import lombok.Data;
import java.io.Serializable;/*** @author 一抹余阳*/
@Data
// 对象要序列化,不然可能会出现乱码
public class UserInfo implements Serializable {private String id;private String nickName;}

4、service

package com.cache.redis.service;import com.cache.redis.entity.UserInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;/*** @author 一抹余阳*/
// @CacheConfig:定义公共设置
@CacheConfig(cacheNames="userInfo2")
public interface UserInfoService {// 1、@Cacheable:定义缓存,用于触发缓存.// 该注解用于标注于方法之上用于标识该方法的返回结果需要被缓存起来,// 标注于类之上标识该类中所有方法均需要将结果缓存起来。// 该注解标注的方法每次被调用前都会触发缓存校验,// 校验指定参数的缓存是否已存在(已发生过相同参数的调用),// 若存在,直接返回缓存结果,否则执行方法内容,最后将方法执行结果保存到缓存中。// 2、key:用于指定当前方法的缓存保存时的键的组合方式,// 默认的情况下使用所有的参数组合而成,这样可以有效区分不同参数的缓存。// 当然我们也可以手动指定,指定的方法是使用SPEL表达式。// #root.methodName -> 即为用当前方法名称做为key// #id -> 即为用入参名称id作为key// #userInfo.id -> 即为用UserInfo对象的id属性作为key// 3、 @Cacheable(cacheNames = "userInfo",key = "#id"),cacheNames也可以单独指定// 4、键值对缓存key,就是说redis缓存的时候key的生成时如下格式:value::key 或 cacheNames::key// 例如,指定value或cacheNames为 userInfo2,key为1,// 即 @Cacheable(value="userInfo2",key="1") 或 @Cacheable(cacheNames = "userInfo",key = "#id")// 生成的缓存key值为 userInfo2::1@Cacheable(key = "#root.methodName")List<UserInfo> findAll();// @CachePut:定义更新缓存,触发缓存更新@Cacheable(key = "#id")UserInfo findById(String id);// @CachePut:该注解用于更新缓存,无论结果是否已经缓存,// 都会在方法执行结束插入缓存,相当于更新缓存,一般用于更新方法之上。@CachePut(key = "#userInfo.id")UserInfo saveUserInfo(UserInfo userInfo);// @CacheEvict:该注解主要用于删除缓存操作// allEntries=true:删除所有缓存// @CacheEvict(key = "#id") 删除id为某值的缓存@CacheEvict(allEntries=true)void delUserInfo(String id);// 扩展:condition属性是用来指定判断条件从而确定是否生成缓存// @Cacheable(value = "userInfo2",key = "#id",condition="#id%2 == 0")// 如果id%2 == 0判断条件成立的话,将会生成redis缓存,即返回true时生成Redis缓存,// 如果EL表达式返回false的话则不生成缓存}

5、service实现类

package com.cache.redis.service.impl;import com.cache.redis.entity.UserInfo;
import com.cache.redis.service.UserInfoService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;/*** @author 一抹余阳* 全部直接模拟数据库操作,省掉dao层,如果需要可以自行添加*/
@Service
public class UserInfoServiceImpl implements UserInfoService {@Overridepublic List<UserInfo> findAll() {System.out.println("查了全部数据。。。");UserInfo userInfo = new UserInfo();userInfo.setId("200");userInfo.setNickName("搬砖小能手");UserInfo userInfo2 = new UserInfo();userInfo2.setId("300");userInfo2.setNickName("我是谁");List<UserInfo> userInfoList = new ArrayList<>();userInfoList.add(userInfo);userInfoList.add(userInfo2);return userInfoList;}@Overridepublic UserInfo findById(String id) {System.out.println("查了一次数据库。。。");UserInfo userInfo = new UserInfo();if("100".equals(id)){userInfo.setNickName("呵呵");userInfo.setId("100");}else{userInfo.setNickName("哈哈");userInfo.setId("101");}return userInfo;}@Overridepublic UserInfo saveUserInfo(UserInfo userInfo) {System.out.println("新增或者修改了一次数据。。。");return userInfo;}@Overridepublic void delUserInfo(String id) {System.out.println("删除了一个数据。。。");}
}

6、redisconfig类

package com.cache.redis.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
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.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;/*** @author 一抹余阳* @create 2020-09-25 10:25*/
@Configuration      //@Configuration:项目启动配置注解
@EnableCaching      //@EnableCaching:开启缓存功能
public class RedisConfig {//@ 读取 application.properties 配置的值@Value("${cache.default.expire-time}")private int defaultExpireTime;@Value("${cache.user.expire-time}")private int userCacheExpireTime;@Value("${cache.user.name}")private String userCacheName;/*** 缓存管理器* @param lettuceConnectionFactory* @return*/@Beanpublic CacheManager cacheManager(RedisConnectionFactory lettuceConnectionFactory) {RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();// 设置缓存管理器管理的缓存的默认过期时间defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(defaultExpireTime))// 设置 key为string序列化.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))// 设置value为json序列化.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))// 不缓存空值.disableCachingNullValues();Set<String> cacheNames = new HashSet<>();cacheNames.add(userCacheName);// 将需要不同过期时间 cacheNames 配置// 对每个缓存空间应用不同的配置,如果所有cacheNames的过期时间都一直,则则不需要特殊配置Map<String, RedisCacheConfiguration> configMap = new HashMap<>();configMap.put(userCacheName, defaultCacheConfig.entryTtl(Duration.ofSeconds(userCacheExpireTime)));RedisCacheManager cacheManager = RedisCacheManager.builder(lettuceConnectionFactory).cacheDefaults(defaultCacheConfig).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();return cacheManager;}
}

7、启动类

package com.cache.redis;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;/*** @author 一抹余阳*/@SpringBootApplication
@PropertySources(value = {@PropertySource(value = {"classpath:application.properties"}, encoding = "utf-8")
})
@Slf4j
public class App {/*** 启动方法* @param args*/public static void main(String[] args) {log.info("ApiMain begin...");try {SpringApplication.run(App.class, args);log.info("App run sucessful!");} catch (Throwable throwable) {log.error("App run error!error msg is {}", throwable.getMessage(), throwable);}}
}

8、测试类

import com.cache.redis.App;
import com.cache.redis.entity.UserInfo;
import com.cache.redis.service.UserInfoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;/***  @author 一抹余阳*  redis缓存测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class CacheTest {@ResourceUserInfoService userInfoService;@Testpublic void findAll(){// 测试缓存:// 第一次查询:没有缓存会先模拟查询数据库 打印 查了全部数据。。。// 然后打印查到的数据// 第二次查询:缓存里有数据,会直接打印缓存的数据List<UserInfo> all = userInfoService.findAll();System.out.println(all);}@Testpublic void queryUserInfo(){// 第一次查询:没有缓存数据会先模拟查询数据库 打印 查了一次数据库。。。// 然后打印模拟查询的数据UserInfo user = userInfoService.findById("100");System.out.println(user);try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}// 第二查询:缓存中有数据,会直接从缓存中查询到数据直接打印,不去模拟数据库查询操作UserInfo user2 = userInfoService.findById("100");System.out.println(user2);}@Testpublic void delUserINfo(){// 如 queryUserInfo() ,缓存中有数据则直接返回缓存中数据,没有模拟查询数据库UserInfo user = userInfoService.findById("100");System.out.println(user);// 删除掉缓存中 id为100 的数据userInfoService.delUserInfo("100");// 再次查询则还需要模拟数据库查询操作UserInfo user2 = userInfoService.findById("100");System.out.println(user2);}@Testpublic void saveOrUpdateUserInfo(){// 不论缓存中有没有数据,都去模拟数据库操作,更新userInfo.id为100的到缓存UserInfo userInfo = new UserInfo();userInfo.setNickName("嘎嘎");userInfo.setId("100");UserInfo userInfo2 = userInfoService.saveUserInfo(userInfo);System.out.println("修改后的数据" + userInfo2);}}

9、项目架构截图

一篇掌握SpringBoot+SpringCache+Redis超详细实例相关推荐

  1. Redis超详细学习

    Redis超详细学习 一.Redis入门 Redis是什么? Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...

  2. SpringBoot整合Mybatis超详细流程

    SpringBoot整合Mybatis超详细流程 文章目录 SpringBoot整合Mybatis超详细流程 前言 详细流程 0.引入Mybatis 1.创建数据 2.创建程序目录 3.理解后台访问流 ...

  3. gateway sentinel 熔断 不起作用_熔断器交流与直流有什么区别?错过这篇文章悔之晚矣!【超详细】上海民熔...

    原标题:熔断器交流与直流有什么区别?错过这篇文章悔之晚矣![超详细]上海民熔 导语 民熔熔断器是利用金属导体作为熔体串联于电路中,当过载或短路电流通过熔体时,因其自身发热而熔断,从而分断电路的一种电器 ...

  4. 【Python爬虫实例学习篇】——5、【超详细记录】从爬取微博评论数据(免登陆)到生成词云

    [Python爬虫实例学习篇]--5.[超详细记录]从爬取微博评论数据(免登陆)到生成词云 个人博客地址:ht/tps://www.asyu17.cn/ 精彩部分提醒: (1)微博评论页详情链接为一个 ...

  5. [Springboot]SpringCache + Redis实现数据缓存

    前言 本文实现了SpringCache + Redis的集中式缓存,方便大家对学习了解缓存的使用. 本文实现: SpringCache + Redis的组合 通过配置文件实现了自定义key过期时间:k ...

  6. Redis 超详细版教程笔记

    视频教程:[狂神说Java]Redis最新超详细版教程通俗易懂 视频地址:https://www.bilibili.com/video/BV1S54y1R7SB 目录索引 nosql 阿里巴巴架构演进 ...

  7. SpringBoot第九篇: springboot整合Redis

    这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis. 引入依赖: 在pom文件中添加redis依赖: <dependen ...

  8. k8s核心组件详细介绍教程(配超详细实例演示)

    本文实验环境基于上篇文章手把手从零开始搭建k8s集群超详细教程 本文根据B站课程云原生Java架构师的第一课K8s+Docker+KubeSphere+DevOps学习总结而来 k8s核心组件介绍 1 ...

  9. Redis数据库和SpringBoot的故事|这一篇就够了(超详细)

最新文章

  1. 怎样理解雷达的相参与非相参
  2. performance and scalability
  3. oracle php 配置,PHP + Oracle的配置
  4. Python小游戏(并夕夕版飞机大战)
  5. 使用番石榴的5个理由
  6. vue : 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确, 然后再试一次。
  7. 架构设计:服务自动化部署和管理流程
  8. scss编译输出css并转换成rem
  9. 10款滑动门代码_jquery 滑动门_js滑动门_tab滑动门_jquery 选项卡_js选项卡_tab选项卡效果(三)
  10. html中的日期框怎么写,HTML5日期输入框(date)
  11. 如何在Mac电脑上打开终端
  12. 笔记本设置wifi热点并抓包
  13. Ubuntu/Debian安装护眼软件f.lux indicator applet
  14. 关于VMware虚拟机中调节图标字体大小
  15. HTML5基础实例(三)
  16. C语言结构体中的冒号用法
  17. 股票中的KD指标金叉和死叉
  18. GStreamer1.0 工具用法
  19. 微信接口开发报错invalid credential, access_token is invalid or not latest hint
  20. ROS学习笔记52--rosbag图片从compressed格式转raw格式代码实现接口介绍

热门文章

  1. Android 背景动画
  2. Java入门04 - 类和方法/继承和多态
  3. 铝合金钻孔加工主轴转速参数及工艺解决方案
  4. 小程序token过期后, 实现无感知的刷新token
  5. 省市联动与Bootstrap的基本使用
  6. Jzoj4877 力场护盾(红警系列)
  7. 掌上围脖LITE版更新到1.1
  8. java之JVM学习全过程学习记录
  9. MD版的花瓣网应用源码
  10. rust太阳能板发电教程_一套完整的简易太阳能发电系统的安装过程演示