SpringBoot整合Redis注意的一些问题
1:ERR value is not an integer or out of range
1-1:背景
使用redisTemplate.opsForValue().increment(key, delat)方法。
1-2:分析
分析:redis对任何不合法的值,都称为ERR。只有使用StringRedisSerializer序列化器才能使用incrment或者decrement方法
1-3:问题解决
使用GenericToStringSerializer、StringRedisSerializer序列化器,都可以使用increment方法.
1-4:建议
建议redis key序列化使用StringRedisSerializer,redis value序列化使用Jackson2JsonRedisSerializer。
1-5:代码示例
/**
* key redis serializer: {@link StringRedisSerializer} and
* key redis serializer: {@link Jackson2JsonRedisSerializer}
**/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
Jackson2JsonRedisSerializer valueRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//设置Redis的value为json格式,并存储对象信息的序列化类型
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
valueRedisSerializer.setObjectMapper(objectMapper); RedisSerializer keyRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(keyRedisSerializer);
template.setValueSerializer(valueRedisSerializer);
template.setHashKeySerializer(keyRedisSerializer);
template.setHashValueSerializer(valueRedisSerializer);
template.setConnectionFactory(factory);
template.afterPropertiesSet(); return template;
}
2:key前面会有一堆\xac\xed\x00\x05t\x00\tb
2-1:分析
分析spring-data的org.springframework.data.redis.core.RedisTemplate源代码以后发现。SpringBoot默认采用defaultSerializer = new JdkSerializationRedisSerializer();来对key,value进行序列化操作,在经过查看JdkSerializationRedisSerializer中对序列化的一系列操作,即默认使用。由于spring操作redis是在jedis客户端基础上进行的,而jedis客户端与redis交互的时候协议中定义是用byte类型交互,jedis中提供了string类型转为byte[]类型。原因其实就出现在这里,解决的办法就是手动定义序列化的方法。

从SpringBoot data redis中RedisTemplate源码可以看出,默认的key/value序列化类是JdkSerializationRedisSerializer。所以会造成持久化时,出现16进制编码的数据。
2-2:解决
使用上面的代码示例即可。key序列化类使用StringRedisSerializer,value序列化类使用Jackson2JsonRedisSerializer即可,采用JSON存储value即可。
SpringBoot整合Redis注意的一些问题的更多相关文章
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合Redis及Redis工具类撰写
SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- SpringBoot系列十:SpringBoot整合Redis
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...
- springboot整合redis(注解形式)
springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...
- springboot整合redis(简单整理)
Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...
- SpringBoot整合redis哨兵主从服务
前提环境: 主从配置 http://www.cnblogs.com/zwcry/p/9046207.html 哨兵配置 https://www.cnblogs.com/zwcry/p/9134721. ...
- springboot整合redis——redisTemplate的使用
一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...
- 九、springboot整合redis二之缓冲配置
1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...
- 三:Springboot整合Redis
一:springboot整合redis redis版本:3.0.0 运行环境:linux 1.安装redis 1.1安装gcc yum install gcc-c++ 1.2解压redis.3.0.0 ...
随机推荐
- 【35.56%】【727A】Transformation: from A to B
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- document.addEventListener的使用介绍
document.addEventListener("事件名称", 函数, false); function 函数名(event){ // 方法执行 } addEventListe ...
- Callable,Runnable异同
1.Runnable和Callable的区别 (1) Callable规定的方法是 call(), Runnable规定的方法是 run(). (2) Callable的任务执行后可返回值,而 Run ...
- VS编译环境中TBB配置和C++中lambda表达式
TBB(Thread Building Blocks),线程构建模块,是由Intel公司开发的并行编程开发工具,提供了对Windows,Linux和OSX平台的支持. TBB for Windows ...
- 简明Python3教程 3.介绍
介绍 Python是少有的几种既强大又简单的编程语言.你将惊喜地发现通过使用Python即可轻松专注于解决问题而非和你所用的语言格式与结构. 下面是Python的官方介绍: Python is an ...
- 脚本 启动/停止 jar包服务
windows (.bat): @set port=8692 @echo %port% for /f "tokens=5" %%i in ('netstat -aon ^| fin ...
- Template简介
分类 ControlTemplate ItemsPanelTemplate DataTemplate 样式Style和模板Template对比 Style:样式,风格Template:模版,某种控 ...
- WPF控件的一些特殊应用
1 checkbox.IsChecked 返回的是bool?类型,需要用bool强转,或者直接和bool类型比较,将发生隐形转换 2 RadioButton有分组属性GroupName
- TCP 的那些事儿(上,下)
http://coolshell.cn/articles/11564.html http://coolshell.cn/articles/11609.html
- 一个字体,大小,颜色可定义的自绘静态框控件-XColorStatic 类(比较好看,一共19篇自绘文章)
翻译来源:https://www.codeproject.com/Articles/5242/XColorStatic-a-colorizing-static-control XColor Stati ...