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 ...
随机推荐
- 信号量 P V测试详解
信号量 当我们编写的程序使用了线程时,不管它是运行在多用户系统上,多进程系统上,还是运行在多用户多进程系统上,我们通常会发现,程序中存在着一部分临界代码,我们需要确保只有一个进程可以进入这个临界代码并 ...
- jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
如果你看了上一篇<ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)>的话,应该就知道我是逼不得已要认真学 ...
- 撩课-Python-每天5道面试题-第2天
一. 简述编程过程中, 注释的作用? (1) 方便开发人员自己理清楚代码思路 因为开发人员在拿到一个需求时, 首先应该思考的是如何将需求问题, 分解成具体的实施步骤; 第一步干啥, 第二步干啥, 第三 ...
- Python Djan 路由对应的名称
路由关系命名 对URL路由关系进行命名,以后可以根据此名称生成自己想要的URL 1. url(r'fdsafdsaeeeee',views.index, name='hello') #给这个url后面 ...
- springcloud 实战 feign使用中遇到的相关问题
springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...
- PoPo数据可视化周刊第3期 - 台风可视化
9月台风席卷全球,本刊特别选取台风最佳可视化案例,数据可视化应用功力最深厚者,当属纽约时报,而传播效果最佳的是The Weather Channel关于Florence的视频预报,运用了数据可视化.可 ...
- float失效的情况
前言:在最近的笔试中,两次碰到类似的问题,什么情况下float会失效?我目前知道的有2种: 1)display:none: 2)position:absolute.fixed. (1)display: ...
- js迪杰斯特拉算法求最短路径
1.后台生成矩阵 名词解释和下图参考:https://blog.csdn.net/csdnxcn/article/details/80057574 double[,] arr = new double ...
- Android深入四大组件(四)Android8.0 根Activity启动过程(前篇)
前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...
- java "Too small initial heap" 错误
Tomcat内存配置 JAVA_OPTS="-server -Duser.timezone=GMT+08-Xms1024m -Xmx1024m -XX:PermSize=1024m -Xmn ...