Thi*_*Lam 12 python django memcached
工具版:
Memcached目前正在运行:
$ ps -ef | grep memcache
nobody 2993 1 0 16:46 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1
Run Code Online (Sandbox Code Playgroud)
我正在使用memcached和python memcached与我的Django proj,我将它设置如下settings.py
:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'TIMEOUT': 86400,
},
}
Run Code Online (Sandbox Code Playgroud)
我在代码中设置了缓存:
from django.core.cache import cache
cache.set('countries', ['Canada', 'US'])
Run Code Online (Sandbox Code Playgroud)
然后我打开一个Django shell来检查缓存的内容:
>>> from django.core.cache import cache
>>> 'countries' in cache
True
>>> import memcache
>>> mc = memcache.Client(['127.0.0.1:11211'], debug=1)
>>> mc.get('countries')
>>>
Run Code Online (Sandbox Code Playgroud)
当我使用Django的缓存时,countries
密钥存在.但是,当我使用Python的memcache时,我没有为国家获得任何东西.上面我做错了什么?
你可以使用memcached_stats:https: //github.com/dlrust/python-memcached-stats
示例:(我使用pylibmc作为缓存,但我认为这应该是相同的是你使用python-memcached)
import pylibmc
from memcached_stats import MemcachedStats
mem = MemcachedStats() # connecting to localhost at default memcached port
# print out all your keys
mem.keys()
# say for example key[0] is 'countries', then to get the value just do
key = mem.keys()[0]
value = mc.get (key)
Run Code Online (Sandbox Code Playgroud)
memcaced_stats还有一个命令行界面:python -m memcached_stats
看看github repo,因为README非常清楚.
归档时间: |
|
查看次数: |
15735 次 |
最近记录: |