Memcached
Settings
Sole
CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', } }
Multi
CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': [ '172.19.26.240:11211', '172.19.26.242:11211', ] } }
BACKEND
MemcachedCache
Install Binding
pip install python-memcached
PyLibMCCache
Install Lib
sudo apt-get install libmemcached-dev # Ubuntu sudo brew install libmemcached # Mac OS X
Install Bingding
pip install pylibmc
Usage
Basic
>>> from django.core.cache import cache >>> cache.set('my_key', 'hello, world!', 30) >>> cache.get('my_key') 'hello, world!'
Problems
- Using python-memcached happens multi server's data outer-sync
- How does django handle multiple memcached servers?
- Solved by changing to use
django.core.cache.backends.memcached.PyLibMCCache
(pylibmc)
- When
List
is too large, set/get is too slow- Use
String
replace ofList
,json.dumps()
before set,json.loads()
after get
- Use
References
[1] Docs@DjangoProject, Django’s cache framework
[2] python-memcached@Github, python-memcached — A python memcached client library
[3] pylibmc@Github, pylibmc — A Python wrapper around the libmemcached interface from TangentOrg