django緩存優化(二)
阿新 • • 發佈:2018-08-27
圖片 類型 world 重復 span tmp spa from cached
一、緩存目的:
1、減小過載
2、避免重復計算
3、提高系統性能
二、如何進行緩存
三、緩存類型
四、緩存粒度分類
五、緩存的設置與使用
示例一:
CACHES = {
‘default‘: {
‘BACKEND‘: ‘django.core.cache.backends.memcached.MemcachedCache‘,
‘LOCATION‘: ‘127.0.0.1:11211‘,
}
}
示例二:
CACHES = { ‘default‘: {‘BACKEND‘: ‘django.core.cache.backends.memcached.MemcachedCache‘,
‘LOCATION‘: ‘unix:/tmp/memcached.sock‘, } }
示例三:
CACHES = {
‘default‘: {
‘BACKEND‘: ‘django.core.cache.backends.memcached.MemcachedCache‘,
‘LOCATION‘: [
‘172.19.26.240:11211‘,
‘172.19.26.242:11211‘,
]
}
}
示例四:
CACHES = {
‘default‘: {
‘BACKEND‘: ‘django.core.cache.backends.memcached.MemcachedCache‘,
‘LOCATION‘: [
‘172.19.26.240:11211‘,
‘172.19.26.242:11212‘,
‘172.19.26.244:11213‘,
]
}
}
訪問緩存:
>>>fromdjango.core.cache import caches >>>cache1 = caches[‘myalias’] >>>cache2 = caches[‘myalias’] >>>cache1 is cache2 True >>>from django.core.cache import cache >>>cache.set(‘my_key’, ‘hello, world’, 30) >>>cache.get(‘my_key’) ‘hello, world!’ >>>cache.get(‘my_key’) None >>>cache.get(‘my_key’,‘has expired’) ‘has expired’
六、緩存原理
django緩存優化(二)