django缓存
网站当访问量加大后会加大数据读取的消耗,而缓存技术能一定程度上缓解这样的消耗。django提供一套完整的缓存机制来供开发者使用。基本原理就是如果访问存在缓存,则返回缓存内容,如果不存在缓存,查询数据库返回数据,并对数据进行缓存
given a URL, try finding that page in the cache
if the page is in the cache:
return the cached page
else:
generate the page
save the generated page in the cache (for next time)
return the generated page
django缓存的几种方式:
数据库缓存'django.core.cache.backends.db.DatabaseCache'
虚拟缓存'django.core.cache.backends.dummy.DummyCache'
文件缓存'django.core.cache.backends.filebased.FileBasedCache'
本地内存缓存'django.core.cache.backends.locmem.LocMemCache'
'django.core.cache.backends.memcached.MemcachedCache'
'django.core.cache.backends.memcached.PyLibMCCache'
sea提供了Redis,Memcached的不错缓存功能,不过由于收费决定先还是采用django自带缓存功能。开始选取的文件缓存机制,结果不支持写文件操作。
后采用了数据库缓存机制,配置代码:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'rgbtime_cache',
'TIMEOUT': 600,
'OPTIONS': {
'MAX_ENTRIES': 1000
}
}
}
调用 python manage.py createcachetable rgbtime_cache 建数据缓存表
'LOCATION': 'rgbtime_cache' 对应数据缓存表名
在需要进行缓存的方法上加装饰语句
from django.views.decorators.cache import cache_page
@cache_page(60 * 15)#缓存15分钟
详情见:
中文django缓存机制 ,django缓存机制 ,
Django 缓存系统
您可能也对下面文章感兴趣:
There are 1 Comments to "django缓存"