以前redis保存json格式数据都是这样写
public boolean set(String key, Object value) {
boolean result = false;
try {
// 使用 JSONObject.toJSONString() 处理要存储的数据
stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(value));
result = true;
} catch (Exception e) {
logger.error("写入reids缓存失败! 错误信息为:", e);
}
return result;
现在改成
/**
* 普通缓存放入
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key,Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
使用时
Demo demo = new Demo();
demo.setAge(AgeEnum.THREE);
demo.setCost(new BigDecimal(200.98));
demo.setMajor("测试的喽");
redis.set("testDemo2", demo);
Demo demo2 = (Demo) redis.get("testDemo2");
System.out.println("取回的对象:" + demo2.getMajor() + "," + demo2.getAge());
运行报错,
Caused by: io.lettuce.core.RedisCommandExecutionException: ERR value is not an integer or out of range
at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:137)
at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:110)
at io.lettuce.core.protocol.AsyncCommand.completeResult(AsyncCommand.java:120)
at io.lettuce.core.protocol.AsyncCommand.complete(AsyncCommand.java:111)
at io.lettuce.core.protocol.CommandHandler.complete(CommandHandler.java:704)
at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:639)
at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:560)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Unknown Source)
需要设置序列化设置
@Configuration
public class LettuceRedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
//创建 redisTemplate 模版
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
//设置 value 的转化格式和 key 的转化格式
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//关联 redisConnectionFactory
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
设置之后运行Ok