Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

spring seesion用redis缓存Reditemplate取值为null

在Spring seesion中,使用由redis缓存保存的实体类。
从Reditemplate取的值是null
但是可以通过stringredistemplate取出内容

如果用Stringredistemplate.Opsforhash().get(K,V),那取出来的内容可以转成实体类吗?
或者怎么使用Reditemplate.Opsforhash().get(K,V)获取到对应的内容
UCEKE~V3URZ98{S0(GGOI97.png

X[codeEHP~@OKK(Y3VBP/code]UT(K.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

能通过 StringRedisTemplate 取得该 key 的 value,而通过 RedisTemplate 得到的是 null 的原因是你是通过 StringRedisTemplate 这个操作类将 value 设置进 redis 的。

StringRedisTemplate 会将 key 序列化,所以只能通过 StringRedisTemplate 获取该 key 的值。

如果想转化为实体类,你可以在 put 是使用 json 工具将实体类转为 json 格式的一个 String 对象。然后在 get 到值之后将 String 反序列化为你想要的实体类。

以上。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...