2016-12-15 59 views
0

我使用rediscloud爲我的django安裝提供memcache支持。 它允許設置SASL認證用戶名和密碼(不知道sasl的東西可能是一個問題)。如何使用django爲memcache指定身份驗證信息?

我沒有找到正確的語法從Django的供應他們:

CACHES = { 
    "default": { 
     "BACKEND": "django.core.cache.backends.memcached.MemcachedCache", 
     "LOCATION": "pub-memcache-********.com:17****", 
     "username": "user", 
     "password": "pass" 
    } 
} 

什麼是正確的語法呢?

編輯:

安裝PyLibMCCache代替,但我無法連接:

在settings.py

os.environ[ 'MEMCACHE_SERVERS' ] = 'pub-****:****' 
os.environ[ 'MEMCACHE_USERNAME' ] = '*' 
os.environ[ 'MEMCACHE_PASSWORD' ] = '*' 

CACHES = { 
    'default': { 
     'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 
     'LOCATION': 'pub-****.com:****', 
     'BINARY' : True, 
    } 
} 

錯誤:

error 3 from memcached_get(:1:ddd-37041): (0x1068af060) CONNECTION FAILURE, ::rec() returned zero, server has disconnected, host: pub-*.com: -> libmemcached/io.cc:484

當我禁用驗證,我沒有得到這個錯誤。

回答

0

您可能需要使用一個外部庫:

https://github.com/django-pylibmc/django-pylibmc

pip install django-pylibmc

他們的README解釋的那樣:從rediscloud人

Django has direct support for pylibmc. [...] Two reasons to use django-pylibmc instead are:

  • You need to use the binary protocol
  • You need to use a username and password to access the memcached server (such as with Memcachier on Heroku).
+0

我的確如此。但我不能捕捉用戶名和密碼。我嘗試在settings.py中添加這個。['MEMCACHE_USERNAME'] ='*' os.environ ['MEMCACHE_PASSWORD'] ='*'或MEMCACHE_USERNAME ='*' MEMCACHE_PASSWORD ='*',它關閉我的連接。 –

+0

根據代碼,這應該足夠了,請參閱https://github.com/django-pylibmc/django-pylibmc/blob/master/django_pylibmc/memcached.py#L70-L71。我會嘗試調試它在那裏插入一個斷點。 – JoseKilo

+0

你會如何設置params.get('USERNAME')?你會在哪裏放USERNAME? –

0

答:

in some environments,binary protocol with authentication is required where bmemcached module which supports memcached binary protocol with authentication.

We request you to please try resolving the error by using django-bmemcached.

To do so, install django-bmemcached:

pip install python-binary-memcached

and,

pip install django-bmemcached

Next, configure your CACHES in the settings.py file:

import os 
import urlparse 
import json 

CACHES = { 
    'default': { 
     'BACKEND': 'django_bmemcached.memcached.BMemcached', 
     'LOCATION': '******.com:1****', 
     'OPTIONS': { 
        'username': 'user', 
        'password': 'pass' 
      } 
    } 
} 

這可以工作,並允許通過認證訪問其memcached服務器。

所以這是另一種使用memcached的替代方案。