8

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 Answer 1

15

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')
'bar'

If cache.get() returns the set value it means that cache is working as it should. Otherwise it will return None.

An other option is to start memcached with $ memcached -vv, since it will log all the cache accesses to the terminal. Or alternatively you can install monitoring tool for memcached (i.e. memcache-top) and check that something is happening in memcached while using your app.

8
  • Blenkus, when I do python manage.py shell, I get bunch of errors: ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named graphite.app_settings Apr 20, 2015 at 17:25
  • Looks like Django can't find settings file. Do you have settings.py file in root directory of your project? Otherwise you have to set DJANGO_SETTINGS_MODULE correctly (in manage.py file). Normaly it is something like <your_project.settings>. Apr 20, 2015 at 18:11
  • Then check this to configure memcached in settings file: docs.djangoproject.com/en/1.8/topics/cache/… Apr 20, 2015 at 18:13
  • Blenkus, I installed python-memcached and in settings.py, I included these lines: if MEMCACHE_HOSTS: CACHES['default'] = { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': MEMCACHE_HOSTS, 'TIMEOUT': DEFAULT_CACHE_DURATION, 'KEY_PREFIX': MEMCACHE_KEY_PREFIX, } and in local_settings, I inclduded these lines: MEMCACHE_HOSTS = ['51.16.48.94:11211'] MEMCACHE_KEY_PREFIX = 'graphite' FIND_CACHE_DURATION = 1500 FIND_TOLERANCE = 2 * FIND_CACHE_DURATION DEFAULT_CACHE_DURATION = 1500, when I recycle apache, I get server 500 err. Apr 20, 2015 at 18:44
  • 1
    "if this works it means that cache is working as it should. Otherwise it would throw error." ... get always returns None for me, and there is no error. Oct 25, 2020 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.