Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you confirm django is using memcached?

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.
like image 586
user1471980 Avatar asked Apr 17 '15 18:04

user1471980


People also ask

How do you use memcached in Django?

This answer explains how to install Memcached on Windows 10 and how to integrate it with Django through a specific client. It was validated using Memcached 1.4. 4, Python 2.7 and Django 1.11. Go to the Django project, start the server and you should get much better results in your Time load.

How do I know if Django cache is working?

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.

How does Django handle cache and memory management?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.


1 Answers

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.

like image 181
Domen Blenkuš Avatar answered Sep 27 '22 19:09

Domen Blenkuš