补习系列(14)-springboot redis 整合-数据读写
目录
- 一、简介
- 二、SpringBoot Redis 读写
- A. 引入 spring-data-redis
- B. 序列化
- C. 读写样例
- 三、方法级缓存
- 四、连接池
- 小结
一、简介
在 补习系列(A3)-springboot redis 与发布订阅 一文中,我们介绍了使用 Redis 实现消息订阅发布的机制,并且给出了一个真实用例。
然而,绝大多数场景下 Redis 是作为缓存被使用的(这是其主要优势)。除此之外,由于Redis 提供了 AOF以及RDB两种持久化机制,某些情况下也可以作为临时数据库使用。
本次将介绍 SpringBoot 中如何使用 Redis 进行缓存读写。
Redis 的基本命令
在学习之前,需要先了解一些Redis 的基本命令,可以参考这里
http://www.redis.cn/
二、SpringBoot Redis 读写
A. 引入 spring-data-redis
添加依赖
<!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>${spring-boot.version}</version></dependency>
spring-boot-starter-redis在1.4版本已经废弃
配置redis连接
application.properties
# redis 连接配置
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.password=
spring.redis.port=6379
spring.redis.ssl=false# 连接池最大数
spring.redis.pool.max-active=10
# 空闲连接最大数
spring.redis.pool.max-idle=10
# 获取连接最大等待时间(s)
spring.redis.pool.max-wait=600000
B. 序列化
同样,我们需要指定 JSON作为 Key/HashKey/Value的主要方式:
/*** 序列化定制* * @return*/@Beanpublic Jackson2JsonRedisSerializer<Object> jackson2JsonSerializer() {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);// 初始化objectmapperObjectMapper mapper = new ObjectMapper();mapper.setSerializationInclusion(Include.NON_NULL);mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(mapper);return jackson2JsonRedisSerializer;}/*** 操作模板* * @param connectionFactory* @param jackson2JsonRedisSerializer* @return*/@Beanpublic RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory connectionFactory,Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(connectionFactory);// 设置key/hashkey序列化RedisSerializer<String> stringSerializer = new StringRedisSerializer();template.setKeySerializer(stringSerializer);template.setHashKeySerializer(stringSerializer);// 设置值序列化template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();// template.setValueSerializer(new// GenericToStringSerializer<Object>(Object.class));return template;}
Jackson2JsonRedisSerializer是Jackson转换的桥接器;
RedisTemplate是用于读写的主要操作类;
C. 读写样例
首先定义一个Pet实体类
public class RedisPet {private String name;private String type;
... ignore get set
利用RedisTemplate封装一层Repository,如下:
@Repositorypublic static class PetRepository {private static final String KEY = "Pets";@Autowiredprivate RedisTemplate<String, Object> redisTemplate;private HashOperations<String, String, Object> hashOperations;@PostConstructprivate void init() {hashOperations = redisTemplate.opsForHash();}public void add(RedisPet pet) {hashOperations.put(KEY, pet.getName(), pet);}public RedisPet find(String name) {return (RedisPet) hashOperations.get(KEY, name);}public Map<String, Object> findAll() {return hashOperations.entries(KEY);}public void clear() {hashOperations.getOperations().delete(KEY);}}
在PetRepository 的实现中,我们利用Hash结构来存储 Pet信息(Pet.name是key)
分别实现了添加(add)/查找(get)/清除(clear)等方法。
最后,实现读写调用:
@Service
public class RedisDataOperation {private static final Logger logger = LoggerFactory.getLogger(RedisDataOperation.class);@Autowiredprivate PetRepository petRepo;@PostConstructpublic void start() {RedisPet pet1 = new RedisPet("Polly", "Bird");RedisPet pet2 = new RedisPet("Tom", "Cat");//写入宠物信息petRepo.add(pet1);petRepo.add(pet2);//打印宠物信息logger.info("polly {}", JsonUtil.toJson(petRepo.find("Polly")));logger.info("pets {}", JsonUtil.toJson(petRepo.findAll()));//清空petRepo.clear();}
上面的代码在应用启动时,会写入两个Pet信息,之后完成清理,控制台输出如下:
RedisDataOperation : polly {"name":"Polly","type":"Bird"}
RedisDataOperation : pets {"Tom":{"name":"Tom","type":"Cat"},"Polly":{"name":"Polly","type":"Bird"}}
三、方法级缓存
除了上面的RedisTemplate,spring-data-redis还提供了方法级缓存,
就是将业务方法的执行结果缓存起来,后面再次调用直接从缓存中取得结果返回。
这种方式可以简化缓存逻辑的代码,比如配置类数据的读取,通过方法注解就可以实现,
下面是一个样例:
/*** 方法级缓存样例* * @author atp**/
@Service
public class RedisCacheOperation {private static final Logger logger = LoggerFactory.getLogger(RedisCacheOperation.class);public static final String PREFIX = "pets:";public static final String WRAP_PREFIX = "'pets:'";/*** 当结果不为空时缓存* * @param name* @return*/@Cacheable(value = "petCache", key = WRAP_PREFIX + "+#name", unless = "#result==null")public RedisPet getPet(String name) {logger.info("get pet {}", name);return new RedisPet(name, "Bird");}/*** 当结果不为空时淘汰缓存* * @param pet* @return*/@CacheEvict(value = "petCache", key = WRAP_PREFIX + "+#pet.name", condition = "#result!=null")public RedisPet updatePet(RedisPet pet) {logger.info("update pet {}", pet.getName());return new RedisPet(pet.getName(), "Bird1");}/*** 当结果为true时淘汰缓存* * @param name* @return*/@CacheEvict(value = "petCache", key = WRAP_PREFIX + "+#name", condition = "#result==true")public boolean deletePet(String name) {logger.info("delete pet {}", name);return true;}
}
涉及到几个注解:
注解 | 说明 |
---|---|
@Cachable | 方法执行结果缓存 |
@CachePut | 方法执行结果缓存(强制) |
@CacheEvict | 方法执行时触发删除 |
其中 @CachePut 与 @Cachable 的区别在于,前者一定会执行方法,并尝试刷新缓存(条件满足),
而后者则是当缓存中不存在时才会执行方法并更新。
注解中的属性 key/condition 都支持通过 Spring EL 表达式来引用参数对象。
启用注解
除了上面的代码,我们还需要使用 @EnableCaching 启用注解:
@EnableCaching
@Configuration
public class RedisConfig {private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);/*** 缓存管理,支持方法级注解* * @param template* @return*/@Beanpublic RedisCacheManager cacheManager(RedisTemplate<String, Object> template) {RedisCacheManager redisCacheManager = new RedisCacheManager(template);// 默认过期时间redisCacheManager.setDefaultExpiration(30 * 60 * 1000);return redisCacheManager;}
当@Cacheable 的key属性为空时,框架会自动生成,格式类似:
param1,param2,param3...
如果希望修改默认的行为,可以使用自定义的 KeyGenerator:
/*** 定制方法缓存的key生成策略** @return*/@Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object target, Method method, Object... args) {StringBuilder sb = new StringBuilder();sb.append(target.getClass().getName());sb.append(method.getName());for (Object arg : args) {sb.append(arg.toString());}return sb.toString();}};}
单元测试
使用一小段单元测试代码来测试方法级缓存功能
@RunWith(SpringRunner.class)
@SpringBootTest(classes =BootSampleRedis.class)
public class RedisCacheOperationTest {private static final Logger logger = LoggerFactory.getLogger(RedisCacheOperationTest.class);@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Autowiredprivate RedisCacheOperation operation;private RedisPet pet1 = new RedisPet("Polly", "Bird");@Testpublic void testGet() {operation.getPet(pet1.getName());Object object = redisTemplate.opsForValue().get(RedisCacheOperation.PREFIX + pet1.getName());logger.info(String.valueOf(object));assertNotNull(object);}@Testpublic void testUpdate() {operation.updatePet(pet1);Object object = redisTemplate.opsForValue().get(RedisCacheOperation.PREFIX + pet1.getName());logger.info(String.valueOf(object));assertNull(object);}@Testpublic void testDelete() {operation.getPet(pet1.getName());// delete cacheoperation.deletePet(pet1.getName());Object object = redisTemplate.opsForValue().get(RedisCacheOperation.PREFIX + pet1.getName());logger.info(String.valueOf(object));assertNull(object);}}
四、连接池
如果希望通过代码来配置 Jedis 的连接池(熟悉的方式),可以声明 JedisConnectionFactory 实现:
/*** 连接池配置** @return*/@Beanpublic JedisConnectionFactory jedisConnectionFactory() {JedisPoolConfig config = new JedisPoolConfig();// 最大连接config.setMaxTotal(10);// 最大空闲,与最大连接保持一致,可减少频繁键链的开销config.setMaxIdle(10);// 连接最大空闲时间config.setMinEvictableIdleTimeMillis(10 * 60 * 1000);// 获取连接等待的最大时长config.setMaxWaitMillis(30000);// 进行空闲连接检测的时间间隔config.setTimeBetweenEvictionRunsMillis(30 * 1000);// 取消不必要的test,有利于性能提升config.setTestOnBorrow(false);![](https://img2018.cnblogs.com/blog/242916/201812/242916-20181206231048870-1133770725.png)config.setTestOnReturn(false);JedisConnectionFactory factory = new JedisConnectionFactory(config);factory.setHostName("127.0.0.1");factory.setPort(6379);logger.info("redis config init first");return factory;}
更多配置可参考这里
示例代码可从 码云gitee 下载。
https://gitee.com/littleatp/springboot-samples/
小结
Redis 在大多数项目中的核心用途是缓存,spring-data-redis 为 SpringBoot 中集成 Redis 读写的封装。
除了 RedisTemplate之外,还实现了方法级的缓存注解,一定程度上简化了业务的使用。
Redis 在分布式系统中的应用场景有很多,后续有机会将进行更多的探讨。
欢迎继续关注"美码师的补习系列-springboot篇" ,期待更多精彩内容^-^
作者:美码师
补习系列(14)-springboot redis 整合-数据读写相关推荐
- 补习系列(13)-springboot redis 与发布订阅
目录 一.订阅发布 常见应用 二.Redis 与订阅发布 三.SpringBoot 与订阅发布 A. 消息模型 B. 序列化 C. 发布消息 D. 接收消息 小结 一.订阅发布 订阅发布是一种常见的设 ...
- 补习系列(15)-springboot 分布式会话原理
目录 一.背景 二.SpringBoot 分布式会话 三.样例程序 四.原理进阶 A. 序列化 B. 会话代理 C. 数据老化 小结 一.背景 在 补习系列(3)-springboot 几种scope ...
- SpringBoot+Redis完成数据缓存(内容丰富度一定超出你的想象)
SpringBoot+Redis完成数据缓存 去年今日此门中 人面桃花相映红 人面不知何处去 桃花依旧笑春风 感谢相遇!感谢自己,努力的样子很给力! 为了更多朋友看见,还是和大家说一声抱歉,限制为粉丝 ...
- STC15系列单片机通过串口多字节数据读写EEPROM操作
STC15系列单片机通过串口多字节数据读写EEPROM操作
- 补习系列(6)- springboot 整合 shiro 一指禅
欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...
- 补习系列(6)-SpringBoot 整合Shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- 补习系列(19)-springboot JPA + PostGreSQL
目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...
- 补习系列(16)-springboot mongodb 数据库应用技巧
目录 一.关于 MongoDB 二.Spring-Data-Mongo 三.整合 MongoDB CRUD A. 引入框架 B. 数据库配置 C. 数据模型 D. 数据操作 E. 自定义操作 四.高级 ...
- 补习系列(2)-springboot mime类型处理
目标 了解http常见的mime类型定义: 如何使用springboot 处理json请求及响应: 如何使用springboot 处理 xml请求及响应: http参数的获取及文件上传下载: 如何获得 ...
最新文章
- hashlib模块使用详情
- 如何快速学习产品?实践才是王道!同为小白的人儿加油!
- 音视频技术开发周刊 | 143
- 文本嵌入的经典模型与最新进展
- 与Xcode相比Adobe AIR开发iOS的优势和局限
- java poi 只能创建?,Java POI使用SS模型创建新的工作簿?
- Keil MDK5 破解
- python代理ip连接失败_遇到问题--python--爬虫--使用代理ip第二次获取代理ip失败
- 椰树集团引爆互联网背后的“流量秘密”
- java 一年有多少周_Java8根据一年中的第几周获得Monday
- matlab中Folder是什么意思,matlab – @folder和文件夹
- 2014年东北四省赛总结
- cbc cryptojs 前后端_AES加密前后端加解密不一致
- 高校房产管理平台架构及安全性需求分析
- 史上最通俗,彻底搞懂字符乱码问题的本质
- word中每页后面的空白怎么删掉
- linux桌面系统之家,Ubuntu下载_Ubuntu DesktopX64标准下载10.10 - 系统之家
- React 生命周期总结(新旧对比)
- 腾讯入股Snap,能救“阅后即焚”的命吗?
- python、Linux、MySQL学习笔记
热门文章
- mysql 多个值求和_SQL优化大神玩转MySQL函数系列(2)LEAST,SUM的应用
- MFC之CString与const char* string 转换
- docker daemon 配置文件
- mybatis-plus排除非表中字段
- vim-录制命令的使用
- 本地wordpress博客系统安装搭建实践
- C# 实现在线软件自动升级自动下载更新文件
- 聯想集團與NBA簽署營銷協議
- 二分答案——跳石头(洛谷 P2678)
- iview兼容ie8_如何解决iview在安卓4.4.4的webview中的兼容性