gpt4 book ai didi

redis - Django_redis 锁失败并出现 UnpicklingError

转载 作者:可可西里 更新时间:2023-11-01 10:53:44 27 4
gpt4 key购买 nike

在我的代码的一个地方,我使用 django_redis 来更新带锁的缓存:

from django.core.cache import cache
with cache.lock('hello'):
# do stuff

在另一个地方,我使用以下方法检查缓存是否未锁定:

if not cache.get('hello'):
# do other stuff

但是,当设置了锁定时,get 调用失败并显示 UnpicklingError:加载 key 无效,'f'。 为什么会发生这种情况?我做错了什么?

您可以使用此代码段重现此行为:

from django.core.cache import cache
with cache.lock('hello'):
cache.get('hello')

最佳答案

不是很明显,但据我了解你的 lock_keycache_key 不能相同。例如这段代码:

cache_key = 'hello'
with cache.lock(cache_key):
cache.get(cache_key)

引发 UnpicklingError: invalid load key...

同时这段代码:

cache_key = 'hello'
lock_key = cache_key + '_lock'
with cache.lock(lock_key):
cache.get(cache_key)

随心所欲。

关于redis - Django_redis 锁失败并出现 UnpicklingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49068381/

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