Django low-level cache API not working
-
18-02-2021 - |
Вопрос
I'm trying to cache the request.POST
dict using the low-level cache API, but it seems to not be working. Instead of the cached dict I get the None
value.
Here's what I tried:
print cache.get('forms_data') # It is None
education_formset = Education(
request.POST or cache.get('forms_data') or None, prefix='education')
if education_formset.is_valid():
if 'view' in request.POST:
cache.set('forms_data', request.POST, 600)
Settings:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:/tmp/memcached.sock',
}
}
There were no exceptions when running the code.
Could it be something wrong with the settings or with the unix memcached.sock
?
Решение
As DrTyrsa points out in the comments, cache.set
returns None.
However, I can't work out what you are trying to achieve here. The cache is global: it's the same for all users of your site. What you're doing here is caching one user's POST values, then retrieving them for all other users. I doubt very much that is what you intend.
If you want to store a user's submissions, keep them in the session.
Не связан с StackOverflow