Why doesn't memcache work in my Django?

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 05:45:36

问题


from django.core.cache import cache
    def testcache():
        cache.set('test','I am putting this message in',3333)
        print cache.get('test')

It just prints "None"

This is in "ps aux":

dovr      2241  0.0  0.8  57824  2144 ?        Ssl  04:20   0:00 memcached -d -u root -m 3900 -p 11211
dovr      2247  0.0  3.7  83696  9800 pts/2    S+   04:20   0:00 python manage.py runserver 0.0.0.0:8000

And this is in my settings.py:

CACHE_BACKEND = 'memcached://MYIPADRESS:11211/'

By the way, it worked before, but not anymore! I don't know why. So weird.


回答1:


You can insure that you can reach memcached from your code by logging value returned from set() method. Probably memcached listens on 127.0.0.1 while you are trying to connect to external interface.




回答2:


Solved.

Django was talking to the server. I did "nc IPADRESS 11211" . And typed "stats"

Then, I looked that cmd_get and cmd_set were changing, so that means it's talking.

Then, I realized that I had problem with code in my views. You can't have "space" in the key. It has to be non-spaceless (and encoded)



来源:https://stackoverflow.com/questions/1550180/why-doesnt-memcache-work-in-my-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!