PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] How do you confirm django is using memcached?

 November 12, 2022     django, memcached     No comments   

Issue

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.

Solution

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.



Answered By - Domen Blenkuš
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing