How do you confirm django is using memcached?

岁酱吖の 提交于 2020-07-08 14:53:05

问题


I have a python django webserver that I am trying to use memcached to make it faster.

I have downloaded and installed memcached and started it a user called virtual as follows:

/usr/local/bin/memcached -u virtual & 

on the django setting.py, I have put the memcached server as this:

MEMCACHE_HOSTS = ['192.168.101.1:11211']

I can do telnet 192.168.101.1 11211 and stats, I do see some statistics there etc.

How do I really know if my django server utilizing the memcached? Is there directory that I can look at or some files to confirm?

Thank you for any insight.

content of manage.py:

#!/usr/bin/env python
import os
import sys

from django.core.management import execute_from_command_line


if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

from the command line:

phthon

from django.core.management import execute_from_command_line

and when I do this:

from django.core.cache import cache

I get these errors:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib64/python2.6/site-packages/django/core/cache/__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/usr/local/lib64/python2.6/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/usr/local/lib64/python2.6/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

回答1:


You can test cache in Django's shell (python manage.py shell):

from django.core.cache import cache
cache.set('foo', 'bar', 600)
cache.get('foo')

if this works it means that cache is working as it should. Otherwise it would throw error.

Other option is to install monitoring tool for memcached (i.e. memcache-top) and check that something is happening in memcached while using your app.



来源:https://stackoverflow.com/questions/29707274/how-do-you-confirm-django-is-using-memcached

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