Spring boot - 整合 Redis缓存(上)
一、配置Pom文件
在使用spring boot 2.0整合redis时遇到了好多问题,网上很多例子都是1.x版本的。故2.0没有折腾好所以将2.0降到了1.5。降级后由于thymeleaf版本也会从默认匹配的3.0降到2.0,所以这里调整了thymeleaf版本,仍指定为3.0 。另mysql也遇到了写问题也在pom中做了调整。调整部分如下:
前:<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version><relativePath /></parent>
后:<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath /></parent>
指定thymeleaf版本否则会随springboot版本发生变化
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.0.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>
指定mysql 版本
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.34</version>
</dependency>
最重要的添加 redis starter依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>1.3.8.RELEASE</version>
</dependency>
二、在配置文件application.properties 中调整版本差异以及添加redis配置
server.context-path=/WebDemo
#server.servlet.context-path=/WebDemo 2.x 配置
redis部分
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
三、redis场景使用
3.1 编写直接使用redis增删查改工具类操作缓存
在项目中新建package com.demo.redis。添加redisConfig 类 ,以及redis工具类RedisServiceImpl类 如下:
@Configuration
@EnableCaching
@EnableAutoConfiguration
public class RedisConfig extends CachingConfigurerSupport{@Beanpublic CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManager rcm = new RedisCacheManager(redisTemplate);//设置缓存过期时间(秒)rcm.setDefaultExpiration(3600);return rcm;}@Bean@ConfigurationProperties(prefix = "spring.redis.pool")public JedisPoolConfig getRedisConfig(){JedisPoolConfig config = new JedisPoolConfig();return config;}@Bean@ConfigurationProperties(prefix = "spring.redis")public JedisConnectionFactory getConnectionFactory() {JedisConnectionFactory factory = new JedisConnectionFactory();factory.setUsePool(true);JedisPoolConfig config = getRedisConfig();factory.setPoolConfig(config);return factory;}@Beanpublic RedisTemplate<?, ?> getRedisTemplate(RedisConnectionFactory factory) {StringRedisTemplate template = new StringRedisTemplate(factory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet(); RedisSerializer stringSerializer = new StringRedisSerializer();template.setKeySerializer(stringSerializer);template.setHashKeySerializer(stringSerializer);return template;}
}
com.demo.redis.RedisServiceImpl
@Service("redisService")
public class RedisServiceImpl{@Resourceprivate RedisTemplate<String, ?> redisTemplate;public boolean set(final String key, final String value) {boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();connection.set(serializer.serialize(key), serializer.serialize(value));return true;}});return result;}public String get(final String key) {String result = redisTemplate.execute(new RedisCallback<String>() {@Overridepublic String doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] value = connection.get(serializer.serialize(key));return serializer.deserialize(value);}});return result;}public boolean expire(final String key, long expire) {return redisTemplate.expire(key, expire, TimeUnit.SECONDS);}public boolean remove(final String key) {boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();connection.del(key.getBytes());return true;}});return result;}
}
在项目中测试使用redis工具设置获取缓存
测试代码:
@Controller
@RequestMapping(value="/course")
public class CourseController {@Autowiredprivate RedisServiceImpl redisService;@RequestMapping(value="/setCourVal",method=RequestMethod.GET)public void setCourVal(String val){redisService.set("testkey", val);}@ResponseBody@RequestMapping(value="/getCourVal",method=RequestMethod.GET)public String getCourVal(String val){return redisService.get("testkey");}
}
测试过程:
首先方式连接进行设置 http://127.0.0.1:8080/WebDemo/course/setCourVal?val=testValue
在进行访问 http://127.0.0.1:8080/WebDemo/course/getCourVal
我么页面也可看到 打印了我们的缓存值 testValue
3.2 如何在项目中对访问的数据接口数据进行缓存呢
在项目中我们主要使用了Spring的缓存注解 @Cacheable、@CachePut、@CacheEvict 来实现缓存功能,具体可以参考下一篇博客进行查看 Spring boot - 整合 Redis缓存(下) https://blog.csdn.net/liuhenghui5201/article/details/90545317
Spring boot - 整合 Redis缓存(上)相关推荐
- Spring Boot基础学习笔记18:Spring Boot整合Redis缓存实现
文章目录 零.学习目标 一.Spring Boot支持的缓存组件 二.基于注解的Redis缓存实现 (一)安装与启动Redis (二)创建Spring Boot项目 - RedisCacheDemo0 ...
- Spring Boot整合Redis缓存(Lettuce)
spring-boot-demo-cache-redis 此 demo 主要演示了 Spring Boot 如何整合 redis,操作redis中的数据,并使用redis缓存数据.连接池使用 Lett ...
- Spring boot - 整合 Redis缓存(下)
在SpringBoot项目中使用Redis进行缓存接口返回数据,以及结合课程表的增删查改进行获取更新缓存. 一.相关注解 @Cacheable.@CachePut.@CacheEvict 在Sprin ...
- 猿创征文 | 微服务 Spring Boot 整合Redis 实战开发解决高并发数据缓存
文章目录 一.什么是 缓存? ⛅为什么用缓存? ⚡如何使用缓存 二.实现一个商家缓存 ⌛环境搭建 ♨️核心源码 ✅测试接口 三.采用 微服务 Spring Boot 注解开启缓存 ✂️@CacheEn ...
- 大聪明教你学Java | Spring Boot 整合 Redis 实现访问量统计
前言 之前开发系统的时候客户提到了一个需求:需要统计某些页面的访问量,记得当时还纠结了一阵子,不知道怎么去实现这个功能,后来还是在大佬的带领下借助 Redis 实现了这个功能.今天又回想起了这件事,正 ...
- Spring Boot整合Redis以及Redis的原理
Redis的原理及知识 Redis简介 redis是一个key-value.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合). ...
- Spring Boot 结合 Redis 缓存
Redis官网: 中:http://www.redis.cn/ 外:https://redis.io/ redis下载和安装 Redis官方并没有提供Redis的Windows版本,这里使用微软提供的 ...
- 十一、Spring Boot整合Redis(一)
Spring Boot整合Redis 1. SpringBoot+单例Redis 1)引入依赖 <dependency> <groupId>org.springf ...
- Spring boot整合Redis(入门教程)
目录 源码分析 jedis VS lettuce 整合测试 导入依赖 配置连接 测试 存入字符串 存入对象 五大数据类型操作 自定义RedisConfig 存入对象 Redis工具类(常用API) 以 ...
最新文章
- php性能分析工具 - xhprof的安装使用
- html特殊字符的html,js,css写法汇总
- java nio 读取图片_给大忙人们看的 Java NIO 极简教程
- 通过系统调用open来查看flag
- mysql分组取日期最大的记录_mysql 分组 group by, 排序 取每条记录中,时间最大
- Jupyter Notebook 快捷键 和 Markdown知识点总结
- sendmail for linux
- docker -v 挂载文件_浅谈关于docker中数据卷的操作,附带案例
- ln: 创建符号链接 “include/asm”: 不支持的操作
- BZOJ1096-[ZJOI2007]仓库建设
- 洛谷 P4001 [ICPC-Beijing 2006]狼抓兔子
- 条件判断_判断疑似陨石应具备什么条件下,才能判断陨石真伪
- Oracle中对时间操作的一些总结
- 万能密码 php,PHP万能密码
- 三菱PLC项目案例学习之PLC控制伺服或步进电机带动丝运行案例
- Windows下linux传盘工具,Windows下安装红旗Linux及工具盘全过程
- tensorflow2 serving
- CSS实现元素width右方向变化、左方向变化、双向变化
- 南宁计算机职称考试网,南宁人事考试职称网
- UML软件开发与建模工具Enterprise Architect发布最新版本v15.2
热门文章
- TQuery组件的Open方法与ExecSQL的区别
- 转载:Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神
- Fences桌面图标分类
- 360浏览器打不开qq空间_刘连康:解决电脑网络正常,浏览器打不开的问题
- html5 dropdownlist,使用HTML5 FindByValue下拉列表(html5 dropdownlist using F
- mysql统计姓名为小明_Mysql 统计查询相同字段只统计一条
- 查询Linux充电时间,【充电】Linux学习(二)——常用的linux命令
- vb devcon获取u盘信息_iOS 13 U盘越狱法,卡代码及U盘终极解决
- Pyton学习—字符串
- 维基解密曝CIA 入侵苹果、安卓机、电视,快来围观8761份泄密文