memcache on django is not working

偶尔善良 提交于 2021-01-27 06:07:53

问题


I have a race condition in Celery. Inspired by this - http://ask.github.io/celery/cookbook/tasks.html#ensuring-a-task-is-only-executed-one-at-a-time I decided to use memcache to add locks to my tasks.

These are the changes I made:

python-memcached

# settings for memcache
CACHES = {
  'default': {
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
    'LOCATION': '127.0.0.1:11211',
   }
}

After this I login to my shell and do the following

>>> import os
>>> import django
>>> from django.core.cache import cache
>>> os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings.base')
>>> cache
<django.core.cache.DefaultCacheProxy object at 0x101e4c860>
>>> cache.set('my_key', 'hello, world!', 30) #display nothing. No T/F
>>> cache.get('my_key') #Display nothing.
>>> from django.core.cache import caches
>>> caches['default']
<django.core.cache.backends.memcached.MemcachedCache object at 0x1048a5208>
>>> caches['default'].set('my_key', 'hello, world!', 30) #display nothing. No T/F
>>> caches['default'].get('my_key') #Display nothing.

also did pip install python-memcached

Using Python 3.6, Django==1.10.5

What am I doing wrong? Any help will be appreciated.


回答1:


The problem was, memcached was killed for some reason and I was assumed it was still running. My bad. Now it works all perfectly.

For anyone who is stuck on a similar problem you want to make sure you are still running memcached, try memcached -vv

Keeping this here for reference.




回答2:


If your install is looking like mine is starting to look (in which case, you have my sympathies; for RHEL is one platform where django seems thinner on-the-ground than this-week's python):

# yum upgrade -y
  yum-config-manager --add-repo=https://dl.fedoraproject.org/pub/epel/7/x86_64/

#  yum install -y emacs-nox
  yum install -y python{-sqlparse,36{,-{devel,pip,pytz,bcrypt}}} memcached

  service memcached start
  chkconfig memcached on

  python36 \
    -m pip install \
      django \
      pymemcache \
    --no-deps --upgrade 

then checking the status on memcache is as easy with the same commands we've been using for 20 years:

  service memcached status

There are other commands one can use on RHEL/EL7 (#fridgeArt), but I prefer compatible workflows in spite of resume-driven differentiation. >:-(

Here's how to launch it with -vv under EL and debuntus too: https://stackoverflow.com/a/22239764/2066657



来源:https://stackoverflow.com/questions/43880976/memcache-on-django-is-not-working

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