Spring Data Redis(sdr)-----序列化
来源:互联网 发布:马赛克复原软件 编辑:程序博客网 时间:2024/05/16 18:13
之前碰到一个问题,这里mark一下:
当redis存储的为hash时,
stackoverflow上的解答见链接
public boolean set(String key,HashMap<String, String> value) { boolean result = false; try { HashOperations<Serializable, String, String> operations = redisTemplate.opsForHash(); operations.putAll(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
这里注意一下,redis里保存的数据类型最好是String,当然你在代码里可以转换成Integer或者Long或者别的去做操作,只是最后往redis存贮时转换成String就行了。存和取根据需求做转换。
当对hash进行increament操作时,出现:
ERR hash value is not an integer; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR hash value is not an integer
这里的increament操作代码是
public void incrBy(String key,String field, Long nm) { BoundHashOperations<Serializable, Object, Object> operations = redisTemplate.boundHashOps(key); operations.increment(field, nm); }
当然代码没错,只是少了对Hash的key和value规定使用org.springframework.data.redis.serializer.StringRedisSerializer的配置。因为当不配置时默认使用jdk的serializer,它就会把hash的key和value(即正常的字符串)序列化后再保存,而StringRedisSerializer保存的是正常的字符串,当然increament也可以递增正常的数字类型字符串(“1”)。
配置如下:
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean>
阅读全文
0 0
- Spring Data Redis(sdr)-----序列化
- spring-data-redis key-value序列化
- spring-data-redis 的序列化问题
- Spring Data redis序列化的比较
- SDR(spring.data.redis)与Sentinel高可用集群Redis客户端Jedis配置
- Redis 使用spring-data-redis的序列化问题
- spring-data-redis中的序列化/反序列化
- 使用kryo作为spring data redis的序列化器
- spring-data-redis使用自定义序列化数据 使用 protobuf
- spring data redis serializer SerializationException 序列化问题
- spring-data-redis increment 命令序列化失败
- spring-data-redis序列化实践笔记
- spring-data-redis 整合,以及使用kryo序列化代替jdk原生序列化机制
- redis:spring-data-redis
- 关于Spring Data redis几种对象序列化的比较
- 关于Spring Data redis几种对象序列化的比较
- spring-data-redis 使用 protobuf进行序列化和反序列
- spring-data-redis 使用过程中需要注意的一点(序列化选择)
- 那些善良的人,一定要看。或许可以帮助你幸免于难
- Python 2.7.x 和 Python 3.x 的主要区别
- TP5自动验证机制
- [USACO08JAN]电话线Telephone Lines 洛谷P1948
- 初探oracle高级队列锁V$LOCK
- Spring Data Redis(sdr)-----序列化
- KLEE error reports
- ECharts 去掉地图(map)的指示图(visualMap)
- 深入理解JAVA集合系列二:ConcurrentHashMap源码解读
- solr历史版本
- 面试题3:数组中重复的数字
- 一个数组中只有两个数字是出现一次,其他所有数字都出现了两次。找出这两个数字
- HDU-1233-还是畅通工程(最小生成树kruscal)
- fork函数与I/O函数之间的交互关系