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

docker:
安装Redis:

查看官方文档:
添加约束

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
此时redis就引入再容器中
可以查看自动配置的类:RedisAutoConfiguration.class
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
} @Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} @Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}

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

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

测试:

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

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

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

测试缓存:

原理:
1、引入redis的starter,容器自动导入的是RedisCacheManage
2、RedisCacheManager帮我们自动创建RedisCache, redis通过操作redis缓存数据的
RedisCacheConfiguration.class

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader()));
List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet(cacheNames));
} return (RedisCacheManager)this.customizerInvoker.customize(builder.build());
}
protected Collection<RedisCache> loadCaches() {
List<RedisCache> caches = new LinkedList();
Iterator var2 = this.initialCacheConfiguration.entrySet().iterator(); while(var2.hasNext()) {
Entry<String, RedisCacheConfiguration> entry = (Entry)var2.next();
caches.add(this.createRedisCache((String)entry.getKey(), (RedisCacheConfiguration)entry.getValue()));
} return caches;
}
 
查询之后。再点击刷新依然是这个页面

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

4、让保存的数据为json
    1.引入redis的starter,cacheManager变为RedisCacheManager
    2.默认创建的RedisCacheManager,再操作数据的     RedisConnectionFactory 
    RedisCacheConfiguration
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader()));
List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet(cacheNames));
} return (RedisCacheManager)this.customizerInvoker.customize(builder.build());
}
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
  3.默认使用的是JdkSerializationRedisSerializer
RedisTemplate.java

public void afterPropertiesSet() {
super.afterPropertiesSet();
boolean defaultUsed = false;
if (this.defaultSerializer == null) {
this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader());
} }
    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. SublimeText3 插件的使用和本身的配置

    --------------------20180109----------------------- Part1:如何设置代码字体变大变小 1.点击菜单栏 Sublime Text 中prefere ...

  2. windbg .net 程序的死锁检测 常用方法(个人备份笔记)

    //死锁检测 .load sosex.dll :> !dlk :> !mk -a The mk command displays a call stack of the currently ...

  3. java设计模式-----8、策略模式

    Strategy模式也叫策略模式是行为模式之一,它对一系列的算法加以封装,为所有算法定义一个抽象的算法接口,并通过继承该抽象算法接口对所有的算法加以封装和实现,具体的算法选择交由客户端决定(策略).S ...

  4. Algorithm——无重复字符的最长子串

    一.问题 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...

  5. 理解域名插槽:slot-scope

    作用域插槽 | 带数据的插槽 最后,就是我们的作用域插槽.这个稍微难理解一点.官方叫它作用域插槽,实际上,对比前面两种插槽,我们可以叫它带数据的插槽.什么意思呢,就是前面两种,都是在组件的templa ...

  6. 线程与全局解释器锁(GIL)

    一.线程概论 1.何为线程 每个进程有一个地址空间,而且默认就有一个控制线程.如果把一个进程比喻为一个车间的工作过程那么线程就是车间里的一个一个流水线. 进程只是用来把资源集中到一起(进程只是一个资源 ...

  7. \n\r 转义字符

    转义字符 意义 ASCII码值(十进制) \a 响铃(BEL) 007 \b 退格(BS) ,将当前位置移到前一列 008 \f 换页(FF),将当前位置移到下页开头 012 \n 换行(LF) ,将 ...

  8. ThreeJS文字作为纹理贴图

    文字作为纹理贴图 From:http://www.linhongxu.com/post/view?id=222 这里可以使用canvas作为纹理贴图,Three为我们提供里CanvasTexture ...

  9. WeinView 与 MITSUBISHI FX 系列 PLC 通讯范例

    1. 范例操作概述 此范例将介绍如何快捷简易地建立WEINVIEW HMI与MITSUBISHI FX系列 PLC通讯. 注意事项:通讯参数设置,通讯线接法. 2. 规划说明 (1) 新建简单 PLC ...

  10. maven(11)-聚合多模块

      聚合项目 一些开源项目,都会把自己的源代码公开到github之类的网站上,我们通过下载其代码,在本地执行maven install,可以把代码编译成jar包安装到本地仓库.而一个项目通常有 ...