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.
user1471980
  • 10,127
  • 48
  • 136
  • 235

1 Answers1

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.

ruohola
  • 21,987
  • 6
  • 62
  • 97
Domen Blenkuš
  • 2,182
  • 1
  • 21
  • 29
  • 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 – user1471980 Apr 20 '15 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 ``````. – Domen Blenkuš Apr 20 '15 at 18:11
  • Then check this to configure memcached in settings file: https://docs.djangoproject.com/en/1.8/topics/cache/#setting-up-the-cache – Domen Blenkuš Apr 20 '15 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. – user1471980 Apr 20 '15 at 18:44
  • This is totaly different topic now :). But anyway, check apache and uwsgi logs for errors. If all is normal, set up logging to file for Django (http://ianalexandr.com/blog/getting-started-with-django-logging-in-5-minutes.html). It would be a lot easier if you would use Django development server for debugging (```python manage.py runserver``` and ```DEBUG = True``` in settings). – Domen Blenkuš Apr 20 '15 at 20:18
  • Blenkus, I have created a new question: http://stackoverflow.com/questions/29774117/how-do-you-point-graphite-to-memcache – user1471980 Apr 21 '15 at 13:51
  • 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. – run_the_race Oct 25 '20 at 13:56
  • @run_the_race I took the liberty to fix that to the answer. – ruohola Jan 04 '21 at 10:28