25、springboot与缓存整合Redis




<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
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的地址:

//操作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);
}

@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);
}

测试缓存:
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;
}


@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;
}
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());
}
}

25、springboot与缓存整合Redis的更多相关文章
- MyBatis功能点一应用:二级缓存整合redis
Mybatis提供了默认的cache实现PerpetualCache,那为什么还要整合第三方的框架redis?因为Mybatis提供的cache实现为单机版,无法实现分布式存储(即本机存储的数据,其他 ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- springboot 2.x整合redis,spring aop实现接口缓存
pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- SpringBoot + MySQL + MyBatis 整合 Redis 实现缓存操作
本地安装 Redis Redis 安装:https://www.cnblogs.com/oukele/p/11373052.html 项目结构: SpringBootRedis 工程项目结构如下: ...
- redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)
平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...
- SpringBoot学习:整合Redis
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 pom.xml添加对redis的依赖: <!-- https://mvnrepos ...
- springboot笔记10——整合Redis
依赖 <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boo ...
- SpringBoot:Shiro 整合 Redis
前言 前段时间做了一个图床的小项目,安全框架使用的是Shiro.为了使用户7x24小时访问,决定把项目由单机升级为集群部署架构.但是安全框架shiro只有单机存储的SessionDao,尽管Shrio ...
- 【快学springboot】13.操作redis之String数据结构
前言 在之前的文章中,讲解了使用redis解决集群环境session共享的问题[快学springboot]11.整合redis实现session共享,这里已经引入了redis相关的依赖,并且通过spr ...
随机推荐
- SublimeText3 插件的使用和本身的配置
--------------------20180109----------------------- Part1:如何设置代码字体变大变小 1.点击菜单栏 Sublime Text 中prefere ...
- windbg .net 程序的死锁检测 常用方法(个人备份笔记)
//死锁检测 .load sosex.dll :> !dlk :> !mk -a The mk command displays a call stack of the currently ...
- java设计模式-----8、策略模式
Strategy模式也叫策略模式是行为模式之一,它对一系列的算法加以封装,为所有算法定义一个抽象的算法接口,并通过继承该抽象算法接口对所有的算法加以封装和实现,具体的算法选择交由客户端决定(策略).S ...
- Algorithm——无重复字符的最长子串
一.问题 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...
- 理解域名插槽:slot-scope
作用域插槽 | 带数据的插槽 最后,就是我们的作用域插槽.这个稍微难理解一点.官方叫它作用域插槽,实际上,对比前面两种插槽,我们可以叫它带数据的插槽.什么意思呢,就是前面两种,都是在组件的templa ...
- 线程与全局解释器锁(GIL)
一.线程概论 1.何为线程 每个进程有一个地址空间,而且默认就有一个控制线程.如果把一个进程比喻为一个车间的工作过程那么线程就是车间里的一个一个流水线. 进程只是用来把资源集中到一起(进程只是一个资源 ...
- \n\r 转义字符
转义字符 意义 ASCII码值(十进制) \a 响铃(BEL) 007 \b 退格(BS) ,将当前位置移到前一列 008 \f 换页(FF),将当前位置移到下页开头 012 \n 换行(LF) ,将 ...
- ThreeJS文字作为纹理贴图
文字作为纹理贴图 From:http://www.linhongxu.com/post/view?id=222 这里可以使用canvas作为纹理贴图,Three为我们提供里CanvasTexture ...
- WeinView 与 MITSUBISHI FX 系列 PLC 通讯范例
1. 范例操作概述 此范例将介绍如何快捷简易地建立WEINVIEW HMI与MITSUBISHI FX系列 PLC通讯. 注意事项:通讯参数设置,通讯线接法. 2. 规划说明 (1) 新建简单 PLC ...
- maven(11)-聚合多模块
聚合项目 一些开源项目,都会把自己的源代码公开到github之类的网站上,我们通过下载其代码,在本地执行maven install,可以把代码编译成jar包安装到本地仓库.而一个项目通常有 ...