3👍
Make sure it’s using the correct cache. Try from django.core.cache import caches
, and then see the contents of caches.all()
. It should just have one instance of django.core.cache.backends.memcached.MemcachedCache
.
If it is, try accessing that directly, e.g.
from django.core.cache import caches
m_cache = caches.all()[0]
m_cache.set("stack","overflow",3000)
m_cache.get("stack")
This might not solve your problem, but will at least get you closer to debugging Memcached instead of Django’s cache proxy or your configuration.
0👍
I believe django augments the key with a version. For example,
django_memcache.set('my_key', 'django', 1000)
will set the key :1:my_key
in memcache:
<36 set :1:my_key 0 1000 6
>36 STORED
However, if you set the key through telnet or python-memcached module, it will store the raw key as expected:
<38 set my_key 0 1000 13
>38 STORED
So, perhaps you are not querying the correct key?
See https://docs.djangoproject.com/en/1.10/topics/cache/#cache-key-transformation
- [Django]-Cross domain at axios
- [Django]-Django apps aren't loaded yet when using asgi
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
-1👍
With set(), set_many(), get(), get_many() and get_or_set(), you can set and get cache values with LocMemCache which is used by default as shown below. *set()
can set a timeout and a version and set_many()
can set multiple cache values with a timeout and get()
can get the key’s cache value matched with the version and get a default value if the key doesn’t exist and get_many()
can get multiple cache values and get_or_set()
can get a key’s cache value if the key exists or set and get a key’s cache value if the key doesn’t exist and my answer explains how to delete cache values with LocMemCache
and the answer of my question explains the default version of a cache value with LocMemCache
:
from django.http import HttpResponse
from django.core.cache import cache
def test(request):
cache.set("first_name", "John")
cache.set("first_name", "David", 30, 2)
cache.set("last_name", "Smith")
cache.set("last_name", "Miller", version=2)
cache.set_many({"age": 36, "gender": "Male"}, 50)
print(cache.get("first_name")) # John
print(cache.get("first_name", version=2)) # David
print(cache.get("first_name", "Doesn't exist", version=3)) # Doesn't exist
print(cache.get_many(["first_name", "last_name", "age", "gender"]))
# {'first_name': 'John', 'last_name': 'Smith', 'age': 36, 'gender': 'Male'}
print(cache.get_many(["first_name", "last_name", "age", "gender"], 2))
# {'first_name': 'David', 'last_name': 'Miller'}
print(cache.get_or_set("first_name", "Doesn't exist")) # John
print(cache.get_or_set("email", "Doesn't exist")) # Doesn't exist
return HttpResponse("Test")
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-How to understand lazy function in Django utils functional module
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3