Reputation: 566
In one place of my code, I use django_redis to update cache with a lock:
from django.core.cache import cache
with cache.lock('hello'):
# do stuff
In another place, I check whether cache is not locked using:
if not cache.get('hello'):
# do other stuff
However, when lock is set, get
call fails with UnpicklingError: invalid load key, 'f'.
Why is this happening? What am I doing wrong?
You can reproduce this behaviour with this snippet:
from django.core.cache import cache
with cache.lock('hello'):
cache.get('hello')
Upvotes: 2
Views: 739
Reputation: 56
it's not very obviously, but as i understand your lock_key
and cache_key
must not be the same. for example this code:
cache_key = 'hello'
with cache.lock(cache_key):
cache.get(cache_key)
raise UnpicklingError: invalid load key...
.
In the same time this code:
cache_key = 'hello'
lock_key = cache_key + '_lock'
with cache.lock(lock_key):
cache.get(cache_key)
works as you want.
Upvotes: 3