当前位置: 首页 > news >正文

(Redis使用系列) SpringBoot中Redis的RedisConfig 二

RedisConfig.java   :     

 对redis提供的两个Template的序列化配置


/**
 * @Author: JCccc
 * @CreateTime: 2018-09-11
 * @Description:
 */
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Configuration
@EnableCaching
public class RedisConfig {
           @Bean
        public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
            RedisCacheConfiguration cacheConfiguration =
                    defaultCacheConfig()
                            .disableCachingNullValues()
                            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Object.class)));
            return RedisCacheManager.builder(connectionFactory).cacheDefaults(cacheConfiguration).build();

//            RedisCacheManager rcm=RedisCacheManager.create(connectionFactory);
//
//            return rcm;

        }


        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(factory);
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            //序列化设置 ,这样为了存储操作对象时正常显示的数据,也能正常存储和获取
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
            return redisTemplate;
        }
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }




}

相关文章:

  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • SpringBoot 定时器简单使用,实现任务调度
  • IDEA 底下的indexing一直自动刷新刷新刷新刷新的解决方案
  • SpringBoot 项目启动出错:Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedde
  • Springboot使用Cookie,生成cookie,获取cookie信息(注解与非注解方式)
  • Springboot最简单的整合使用Mongodb案例
  • IDEA 设置 快速生成serialVersionUID
  • springboot 最简单的日志打印,输出日志文件 及全局异常捕获处理
  • SpringBoot 最简单的使用异步线程案例 @Async
  • Springboot中 使用request的几种方法。
  • 使用WebStorm 报错解决方法!!Unable to save settings: Failed to save settings (内含注册码破解码)
  • Angular7 关于Bootstrap4.x.x版本无法使用Glyphicons 字体图标 简单解决方案
  • springboot 获取访问接口的请求的IP地址
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • 什么是Kafka
  • 2017届校招提前批面试回顾
  • Angular 2 DI - IoC DI - 1
  • axios请求、和返回数据拦截,统一请求报错提示_012
  • C++类的相互关联
  • CentOS7 安装JDK
  • iOS 系统授权开发
  • Less 日常用法
  • mysql中InnoDB引擎中页的概念
  • Python_OOP
  • vue学习系列(二)vue-cli
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 从0实现一个tiny react(三)生命周期
  • 面试遇到的一些题
  • 前端代码风格自动化系列(二)之Commitlint
  • 提醒我喝水chrome插件开发指南
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 白色的风信子
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • (02)Hive SQL编译成MapReduce任务的过程
  • (zt)最盛行的警世狂言(爆笑)
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (一)u-boot-nand.bin的下载
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (转) ns2/nam与nam实现相关的文件
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .htaccess配置常用技巧
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .NET6 命令行启动及发布单个Exe文件
  • .NET企业级应用架构设计系列之开场白
  • @RequestMapping 的作用是什么?
  • [ Algorithm ] N次方算法 N Square 动态规划解决
  • [ 云计算 | AWS ] 对比分析:Amazon SNS 与 SQS 消息服务的异同与选择
  • [Android]竖直滑动选择器WheelView的实现
  • [Angular] 笔记 9:list/detail 页面以及@Output
  • [AutoSAR系列] 1.3 AutoSar 架构
  • [Effective C++读书笔记]0012_复制对象时勿忘其每一部分
  • [GYCTF2020]Ez_Express
  • [JavaWeb]——获取请求参数的方式(全面!!!)