gpt4 book ai didi

redis - 如何消除通过 redis get 命令获取垃圾值?

转载 作者:IT王子 更新时间:2023-10-29 06:11:24 25 4
gpt4 key购买 nike

问题:

  • 我正在为 redis get 调用获取诸如“OK”之类的垃圾值。
  • 无论尝试通过 get 命令获取的 key 如何,此问题在特定时间段内经常重现。

我正在使用:

  • Redis 2.8 版
  • Jedis客户端2.5.1连接Redis

请提出一些解决方案来解决此问题。

最佳答案

this page 中概述了该问题.来自文章:

I learned a hard lesson when enabling Redis transactions in the Spring RedisTemplate class redisTemplate.setEnableTransactionSupport(true);: Redis started returning junk data after running for a few days, causing serious data corruption. A similar case was reported on StackOverflow.

By running a monitor command, my team discovered that after a Redis operation or RedisCallback, Spring doesn't close the Redis connection automatically, as it should do. Reusing an unclosed connection may return junk data from an unexpected key in Redis. Interestingly, this issue doesn't show up when transaction support is set to false in RedisTemplate.

We discovered that we could make Spring close Redis connections automatically by configuring a PlatformTransactionManager (such as DataSourceTransactionManager) in the Spring context, then using the @Transactional annotation to declare the scope of Redis transactions.

Based on this experience, we believe it's good practice to configure two separate RedisTemplates in the Spring context: One with transaction set to false is used on most Redis operations; the other with transaction enabled is only applied to Redis transactions. Of course PlatformTransactionManager and @Transactional must be declared to prevent junk values from being returned.

Moreover, we learned the downside of mixing a Redis transaction with a relational database transaction, in this case JDBC. Mixed transactions do not behave as you would expect.

关于redis - 如何消除通过 redis get 命令获取垃圾值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34992256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com