默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中

docker:
安装Redis:

查看官方文档:
添加约束

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
此时redis就引入再容器中
可以查看自动配置的类:RedisAutoConfiguration.class
  1. public class RedisAutoConfiguration {
  2. public RedisAutoConfiguration() {
  3. }
  4.  
  5. @Bean
  6. @ConditionalOnMissingBean(
  7. name = {"redisTemplate"}
  8. )
  9. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  10. RedisTemplate<Object, Object> template = new RedisTemplate();
  11. template.setConnectionFactory(redisConnectionFactory);
  12. return template;
  13. }
  14.  
  15. @Bean
  16. @ConditionalOnMissingBean
  17. public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  18. StringRedisTemplate template = new StringRedisTemplate();
  19. template.setConnectionFactory(redisConnectionFactory);
  20. return template;
  21. }
  22. }

在配置文件中引入redis的地址:

测试:
此时两个操作redis的类都在容器中:
  1. //操作kv键值对的
  2. @Autowired
  3. RedisTemplate redisTemplate;
  4.  
  5. //操作kv都是字符串
  6. @Autowired
  7. StringRedisTemplate stringRedisTemplate;

测试:

测试添加对象:
 
对象实现类需要实现序列化
  1. public class Employee implements Serializable {
  1. //测试保存对象
  2. @Test
  3. public void test2(){
  4. //保存的是emp的对象
  5. Employee emp = employeeMapper.getEmpById();
  6. //保存的是employee的对象
  7. //默认如果使用保存对象,使用jdk序列化机制,序列化后的数据保存在redis中
  8. redisTemplate.opsForValue().set("emp01",emp);
  9. }

//测试天对保存对象2
 
首先是自动一序列化器
  1. @Configuration
  2. public class redisConfig {
  3. //专门序列化Employee
  4. @Bean
  5. public RedisTemplate<Object, Employee> redisTemplateEmp(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  6. RedisTemplate<Object, Employee> template = new RedisTemplate();
  7. template.setConnectionFactory(redisConnectionFactory);
  8.  
  9. Jackson2JsonRedisSerializer<Employee> json = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
  10. template.setDefaultSerializer(json);
  11. return template;
  12. }
  13. }

  1. @Autowired
  2. RedisTemplate redisTemplateEmp;
  3.  
  4. //测试保存对象
  5. @Test
  6. public void test2(){
  7. //保存的是emp的对象
  8. Employee emp = employeeMapper.getEmpById();
  9. //将数据以json的方式
  10. //实现redisTemplate默认的序列化规则,改变默认的序列化规则
  11. redisTemplateEmp.opsForValue().set("emp1",emp);
  12. }

测试缓存:

原理:
1、引入redis的starter,容器自动导入的是RedisCacheManage
2、RedisCacheManager帮我们自动创建RedisCache, redis通过操作redis缓存数据的
  1. RedisCacheConfiguration.class
  2.  
  3. @Bean
  4. public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
  5. RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader()));
  6. List<String> cacheNames = this.cacheProperties.getCacheNames();
  7. if (!cacheNames.isEmpty()) {
  8. builder.initialCacheNames(new LinkedHashSet(cacheNames));
  9. }
  10.  
  11. return (RedisCacheManager)this.customizerInvoker.customize(builder.build());
  12. }
  1. protected Collection<RedisCache> loadCaches() {
  2. List<RedisCache> caches = new LinkedList();
  3. Iterator var2 = this.initialCacheConfiguration.entrySet().iterator();
  4.  
  5. while(var2.hasNext()) {
  6. Entry<String, RedisCacheConfiguration> entry = (Entry)var2.next();
  7. caches.add(this.createRedisCache((String)entry.getKey(), (RedisCacheConfiguration)entry.getValue()));
  8. }
  9.  
  10. return caches;
  11. }
 
查询之后。再点击刷新依然是这个页面

3、默认保存数据都是k-v都是object,利用序列化来保存   
查看缓存:

4、让保存的数据为json
    1.引入redis的starter,cacheManager变为RedisCacheManager
    2.默认创建的RedisCacheManager,再操作数据的     RedisConnectionFactory 
    RedisCacheConfiguration
  1. @Bean
  2. public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
  3. RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader()));
  4. List<String> cacheNames = this.cacheProperties.getCacheNames();
  5. if (!cacheNames.isEmpty()) {
  6. builder.initialCacheNames(new LinkedHashSet(cacheNames));
  7. }
  8.  
  9. return (RedisCacheManager)this.customizerInvoker.customize(builder.build());
  10. }
  1. public class RedisAutoConfiguration {
  2. @Bean
  3. @ConditionalOnMissingBean(
  4. name = {"redisTemplate"}
  5. )
  6. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  7. RedisTemplate<Object, Object> template = new RedisTemplate();
  8. template.setConnectionFactory(redisConnectionFactory);
  9. return template;
  10. }
  3.默认使用的是JdkSerializationRedisSerializer
  1. RedisTemplate.java
  2.  
  3. public void afterPropertiesSet() {
  4. super.afterPropertiesSet();
  5. boolean defaultUsed = false;
  6. if (this.defaultSerializer == null) {
  7. this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader());
  8. }
  9.  
  10. }
    4.自定义
springboot的1.5

25、springboot与缓存整合Redis的更多相关文章

  1. MyBatis功能点一应用:二级缓存整合redis

    Mybatis提供了默认的cache实现PerpetualCache,那为什么还要整合第三方的框架redis?因为Mybatis提供的cache实现为单机版,无法实现分布式存储(即本机存储的数据,其他 ...

  2. 【快学springboot】11.整合redis实现session共享

    前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...

  3. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  4. SpringBoot + MySQL + MyBatis 整合 Redis 实现缓存操作

    本地安装 Redis Redis 安装:https://www.cnblogs.com/oukele/p/11373052.html 项目结构:  SpringBootRedis 工程项目结构如下: ...

  5. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  6. SpringBoot学习:整合Redis

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 pom.xml添加对redis的依赖: <!-- https://mvnrepos ...

  7. springboot笔记10——整合Redis

    依赖 <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boo ...

  8. SpringBoot:Shiro 整合 Redis

    前言 前段时间做了一个图床的小项目,安全框架使用的是Shiro.为了使用户7x24小时访问,决定把项目由单机升级为集群部署架构.但是安全框架shiro只有单机存储的SessionDao,尽管Shrio ...

  9. 【快学springboot】13.操作redis之String数据结构

    前言 在之前的文章中,讲解了使用redis解决集群环境session共享的问题[快学springboot]11.整合redis实现session共享,这里已经引入了redis相关的依赖,并且通过spr ...

随机推荐

  1. 信号量 P V测试详解

    信号量 当我们编写的程序使用了线程时,不管它是运行在多用户系统上,多进程系统上,还是运行在多用户多进程系统上,我们通常会发现,程序中存在着一部分临界代码,我们需要确保只有一个进程可以进入这个临界代码并 ...

  2. jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)

    如果你看了上一篇<ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)>的话,应该就知道我是逼不得已要认真学 ...

  3. 撩课-Python-每天5道面试题-第2天

    一. 简述编程过程中, 注释的作用? (1) 方便开发人员自己理清楚代码思路 因为开发人员在拿到一个需求时, 首先应该思考的是如何将需求问题, 分解成具体的实施步骤; 第一步干啥, 第二步干啥, 第三 ...

  4. Python Djan 路由对应的名称

    路由关系命名 对URL路由关系进行命名,以后可以根据此名称生成自己想要的URL 1. url(r'fdsafdsaeeeee',views.index, name='hello') #给这个url后面 ...

  5. springcloud 实战 feign使用中遇到的相关问题

    springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...

  6. PoPo数据可视化周刊第3期 - 台风可视化

    9月台风席卷全球,本刊特别选取台风最佳可视化案例,数据可视化应用功力最深厚者,当属纽约时报,而传播效果最佳的是The Weather Channel关于Florence的视频预报,运用了数据可视化.可 ...

  7. float失效的情况

    前言:在最近的笔试中,两次碰到类似的问题,什么情况下float会失效?我目前知道的有2种: 1)display:none: 2)position:absolute.fixed. (1)display: ...

  8. js迪杰斯特拉算法求最短路径

    1.后台生成矩阵 名词解释和下图参考:https://blog.csdn.net/csdnxcn/article/details/80057574 double[,] arr = new double ...

  9. Android深入四大组件(四)Android8.0 根Activity启动过程(前篇)

    前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...

  10. java "Too small initial heap" 错误

    Tomcat内存配置 JAVA_OPTS="-server -Duser.timezone=GMT+08-Xms1024m -Xmx1024m -XX:PermSize=1024m -Xmn ...