【问题标题】:memcache on django is not workingdjango 上的内存缓存不起作用
【发布时间】:2017-10-08 10:09:45
【问题描述】:

我在Celery 中有一个竞争条件。受此启发 - http://ask.github.io/celery/cookbook/tasks.html#ensuring-a-task-is-only-executed-one-at-a-time 我决定使用 memcache 为我的任务添加锁。

这些是我所做的更改:

python-memcached

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

在此之后我登录到我的 shell 并执行以下操作

>>> 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.

也做了pip install python-memcached

使用Python 3.6Django==1.10.5

我做错了什么?任何帮助将不胜感激。

【问题讨论】:

    标签: django memcached celery django-cache python-memcached


    【解决方案1】:

    问题是,memcached 因某种原因被杀死,我认为它仍在运行。我的错。 现在一切正常。

    对于遇到类似问题的任何人,您想确保自己仍在运行 memcached,请尝试memcached -vv

    将其保留在这里以供参考。

    【讨论】:

      【解决方案2】:

      如果你的安装看起来像我的一样(在这种情况下,你有我的同情;因为 RHEL 是一个平台,django 看起来比这周的 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 
      

      然后使用我们使用了 20 年的相同命令来检查 memcache 上的状态同样简单:

        service memcached status
      

      在 RHEL/EL7 (#fridgeArt) 上可以使用其他命令,但我更喜欢兼容的工作流程,尽管恢复驱动的差异化。 >:-(

      下面是如何在 EL 和 debuntus 下使用 -vv 启动它:https://stackoverflow.com/a/22239764/2066657

      【讨论】:

        猜你喜欢
        • 2020-12-21
        • 1970-01-01
        • 2011-10-25
        • 1970-01-01
        • 2011-12-19
        • 2018-02-12
        • 2016-09-26
        • 2019-03-25
        相关资源
        最近更新 更多