最近在入门SpringBoot,然后在感慨 SpringBoot较于Spring真的方便多时,顺便记录下自己在集成redis时的一些想法。

1、从springboot官网查看redis的依赖包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、操作redis

/*
操作k-v都是字符串的
*/
@Autowired
StringRedisTemplate stringRedisTemplet; /*
操作k-v都是对象的
*/
@Autowired
RedisTemplate redisTemplate;

redis的包中提供了两个可以操作方法,根据不同类型的值相对应选择。

两个操作方法对应的redis操作都是相同的

 stringRedisTemplet.opsForValue() // 字符串
stringRedisTemplet.opsForList() // 列表
stringRedisTemplet.opsForSet() // 集合
stringRedisTemplet.opsForHash() // 哈希
stringRedisTemplet.opsForZSet() // 有序集合

3、修改数据的存储方式

在StringRedisTemplet中,默认都是存储字符串的形式;在RedisTemplet中,值可以是某个对象,而redis默认把对象序列化后存储在redis中(所以存放的对象默认情况下需要序列化)

如果需要更改数据的存储方式,如采用json来存储在redis中,而不是以序列化后的形式。

1)自己创建一个RedisTemplate实例,在该实例中自己定义json的序列化格式(org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer)

// 这里传入的是employee对象(employee 要求可以序列化)
Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);

2)把定义的格式放进自己定义的RedisTemplate实例中

RedisTemplate<Object,Employee> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 定义格式
Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
// 放入RedisTemplate实例中
template.setDefaultSerializer(jackson2JsonRedisSerializer);

参考代码:

@Bean
public RedisTemplate<Object,Employee> employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException{
RedisTemplate<Object,Employee> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(jackson2JsonRedisSerializer);
return template;
}

原理:

@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
} @Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
) // 在容器当前没有redisTemplate时运行
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} @Bean
@ConditionalOnMissingBean // 在容器当前没有stringRedisTemplate时运行
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}

如果你自己定义了RedisTemplate后并添加@Bean注解,(要在配置类中定义),那么默认的RedisTemplate就不会被添加到容器中,运行的就是自己定义的ReidsTemplate实例,而你在实例中自己定义了序列化格式,所以就会以你采用的格式定义存放在redis中的对象。

4、更改默认的缓冲

springboot默认提供基于注解的缓冲,只要在主程序类(xxxApplication)标注@EnableCaching,缓冲注解有

@Cachingable、@CachingEvict、@CachingPut,并且该缓冲默认使用的是ConcurrentHashMapCacheManager

当引入redis的starter后,容器中保存的是RedisCacheManager ,RedisCacheManager创建RedisCache作为缓冲组件,RedisCache通过操纵redis缓冲数据

5、修改redis缓冲的序列化机制

在SpringBoot中,如果要修改序列化机制,可以直接建立一个配置类,在配置类中自定义CacheManager,在CacheManager中可以自定义序列化的规则,默认的序列化规则是采用jdk的序列化

注:在SpringBoot 1.5.6 和SpringBoot 2.0.5 的版本中自定义CacheManager存在差异

参考代码:

// springboot 1.x的版本
public RedisCacheManager employeeCacheManager(RedisConnectionFactory redisConnectionFactory){ // 1、自定义RedisTemplate
RedisTemplate<Object,Employee> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(jackson2JsonRedisSerializer); // 2、自定义RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(template);
cacheManager.setUsePrefix(true); // 会将CacheName作为key的前缀 return cacheManager;
} // springboot 2.x的版本 /**
* serializeKeysWith() 修改key的序列化规则,这里采用的是StringRedisSerializer()
* serializeValuesWith() 修改value的序列化规则,这里采用的是Jackson2JsonRedisSerializer<Employee>(Employee.class)
* @param factory
* @return
*/
@Bean
public RedisCacheManager employeeCacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<Employee>(Employee.class))); RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build(); return cacheManager;
}

tip:可以通过查看各版本的org.springframework.data.redis.cache.RedisCacheConfiguration去自定义CacheManager.

因为不同版本的SpringBoot对应的Redis版本也是不同的,所以要重写时可以查看官方是怎么定义CacheManager,才知道怎样去自定义CacheManager。

Springboot 2.0 - 集成redis的更多相关文章

  1. SpringBoot(十)_springboot集成Redis

    Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...

  2. 【转载】Springboot整合 一 集成 redis

    原文:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html https://blog.csdn.net/plei_ ...

  3. 【原创】SpringBoot 2.7.0通过lettuce及commons-pool2 v2.9.0集成Redis踩坑记录

    背景 公司的一个项目由于HTTPS证书到期,导致小程序.POS不能正常使用.所以百度了下,通过URL检测证书有效期的代码,并自行整合到一个服务中. 代码仓库:[基于SpringBoot + 企业微信 ...

  4. springboot 2.X 集成redis

    在实际开发中,经常会引入redis中间件做缓存,这里介绍springboot2.X后如何配置redis 1 Maven中引入redis springboot官方通过spring-boot-autoco ...

  5. springboot 2.0 自定义redis自动装配

    首先创建maven项目 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xm ...

  6. SpringBoot 2.0集成spring-data-elasticsearch

    1 资料 https://segmentfault.com/a/1190000015568618 https://github.com/JeffLi1993/springboot-learning-e ...

  7. SpringBoot 2.0 集成 JavaMail ,实现异步发送邮件

    一.JavaMail的核心API 1.API功能图解 2.API说明 (1).Message 类: javax.mail.Message 类是创建和解析邮件的一个抽象类 子类javax.mail.in ...

  8. SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理

    一.Redis简介 Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elastic ...

  9. spring boot集成redis的血泪史

    首先说明环境不是我搭建的,然后因项目需要添加redis的时候,麻烦来了.springboot 用的是1.5.9因为以前弄过redis,所以直接拿过来,麻烦了首先是莫名的错误,连项目都启动不了.但是最后 ...

随机推荐

  1. Java编程思想 学习笔记9

    九.接口 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 1.抽象类和抽象方法  抽象类是普通的类与接口之间的一种中庸之道.创建抽象类是希望通过这个通用接口操纵一系列类. Java提 ...

  2. 〖C语言学习笔记 〗(二) 数据类型

    前言 本文为c语言的学习笔记,很多只是留下来占位的 数据类型 助记:变量就是在内存中挖个坑并给这个坑命名,而数据类型就是挖内存的坑的尺寸 基础类型 整数类型: short int int long i ...

  3. JAVA单链表的实现-不带头结点且没有尾指针

    本程序采用JAVA语言实现了线性表的链式实现.首先定义了线性表的接口ListInterface,然后LList类实现了ListInterface完成了链表的实现. 本实现中,链表是不带表头结点的,且有 ...

  4. JacobMathType

    JACOB是一个 Java到微软的COM接口的桥梁.使用JACOB允许任何JVM访问COM对象,从而使JAVA应用程序能够调用COM对象,;MathType 是由美国Design Science公司开 ...

  5. 在html中控制自动换行

      其实只要在表格控制中添加一句<td style="word-break:break-all">就搞定了.其中可能对英文换行可能会分开一个单词问题:解决如下:语法: ...

  6. u-boot移植(十三)---代码修改---裁剪及环境变量 二

    一.错误处理 上一节遇到一个错误: print一下: 发现我们在jz2440.h中静态写的网络参数都没有写进去. dm9000 address not set. dm9000的地址未设置. 这里对应两 ...

  7. 嵌入式iframe子页面与父页面js通信方式

    iframe框架中的页面与主页面之间的通信方式根据iframe中src属性是同域链接还是跨域链接,有明显不同的通信方式,同域下的数据交换和DOM元素互访就简单的多了,而跨域的则需要一些巧妙的方式来实现 ...

  8. 前端mock数据的几种方式

    方式 备注 本地php服务架设 直接输出json 使用在线mock服务 如easyMock.apizza.Rap1\2 , 可以远程协作  本地node服务:koa+mongodb    本地node ...

  9. mysql 8.0 ~ 安装

    1 环境配置   wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar. ...

  10. jquery 学习(七) - 常用动态效果

    <!--转载于 听说你的代码很6--><!--http://www.jq22.com/webqd2377--> CSS <style> #content #firs ...