Django中如何配置Database缓存?
2016-11-02 18:09
423 查看
BACKEND:
django.core.cache.backends.db.DatabaseCache
LOCATION:
数据库表名
示例:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
Database缓存配置——创建缓存表
使用数据库缓存之前,需要创建缓存表:
python manage.py createcachetable
创建的表名为缓存配置中的LOCATION值
思考:Cache表创建到哪里去了呢?
配置好数据库缓存之前还需要配置数据库
示例:
DATABASES = {
'default': {
'NAME': 'app_data',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'postgres_user',
'PASSWORD': ’’password
}
}
只会创建不存在的表,如果表已经存在,不操作 默认情况下会将cache表创建到default数据库,如果要创建到其他数据库,需要指定database
疑问?
1)可否将cache分类存储到不同的表?
2)可否将cache分别存储到不同数据库?
答案:可以的
1)Mutiple Database Caches: createcachetable会为每个 Cache 创建一张缓存表
2)Mutiple Databases: createcachetable会去查找数据库routers中的allow_migrate()方法,检查是否允许migrate。
3)createcachetable默认使用default数据库,要使用其它数据库,需要用--database参数。
Multi Database Caches配置示例
CACHES = {
’default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': ’api_cache_table',
},# 必须配置default
‘apps’: {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': ’apps_cache_table',
}
}
如果要使用缓存,必须配置default缓存,这里的示例又配置了apps这个缓存
class CacheRouter(object):
"""A router to control all database cache operations"""
def db_for_read(self, model, **hints):
"All cache read operations go to the replica"
…
def db_for_write(self, model, **hints):
"All cache write operations go to primary"
…
def allow_migrate(self, db, app_label, model_name=None, **hints):
"Only install the cache model on primary"
…
dbRouter控制缓存的读写,需要分别实现db_for_read,db_for_write和allow_migrate这三个方法
1.Multiple Databases——读
def db_for_read(self, model, **hints):
"All cache read operations go to the replica"
if model._meta.app_label == 'django_cache':
return 'cache_replica'
return None
所有DB缓存的读操作指向 cache_replica DB
2.Multiple Databases——写
def db_for_write(self, model, **hints):
"All cache write operations go to primary"
if model._meta.app_label == 'django_cache':
return 'cache_primary'
return None
所有DB缓存的写操作指向 cache_primary DB
3.Multiple Databases——migration
def allow_migrate(self, db, app_label, model_name=None, **hints):
"Only install the cache model on primary"
if app_label == 'django_cache':
return db == 'cache_primary'
return None
允许在 cache_primary DB 上执行 migration
原文链接:http://www.maiziedu.com/wiki/django/database/
django.core.cache.backends.db.DatabaseCache
LOCATION:
数据库表名
示例:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
Database缓存配置——创建缓存表
使用数据库缓存之前,需要创建缓存表:
python manage.py createcachetable
创建的表名为缓存配置中的LOCATION值
思考:Cache表创建到哪里去了呢?
配置好数据库缓存之前还需要配置数据库
示例:
DATABASES = {
'default': {
'NAME': 'app_data',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'postgres_user',
'PASSWORD': ’’password
}
}
只会创建不存在的表,如果表已经存在,不操作 默认情况下会将cache表创建到default数据库,如果要创建到其他数据库,需要指定database
疑问?
1)可否将cache分类存储到不同的表?
2)可否将cache分别存储到不同数据库?
答案:可以的
1)Mutiple Database Caches: createcachetable会为每个 Cache 创建一张缓存表
2)Mutiple Databases: createcachetable会去查找数据库routers中的allow_migrate()方法,检查是否允许migrate。
3)createcachetable默认使用default数据库,要使用其它数据库,需要用--database参数。
Multi Database Caches配置示例
CACHES = {
’default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': ’api_cache_table',
},# 必须配置default
‘apps’: {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': ’apps_cache_table',
}
}
如果要使用缓存,必须配置default缓存,这里的示例又配置了apps这个缓存
class CacheRouter(object):
"""A router to control all database cache operations"""
def db_for_read(self, model, **hints):
"All cache read operations go to the replica"
…
def db_for_write(self, model, **hints):
"All cache write operations go to primary"
…
def allow_migrate(self, db, app_label, model_name=None, **hints):
"Only install the cache model on primary"
…
dbRouter控制缓存的读写,需要分别实现db_for_read,db_for_write和allow_migrate这三个方法
1.Multiple Databases——读
def db_for_read(self, model, **hints):
"All cache read operations go to the replica"
if model._meta.app_label == 'django_cache':
return 'cache_replica'
return None
所有DB缓存的读操作指向 cache_replica DB
2.Multiple Databases——写
def db_for_write(self, model, **hints):
"All cache write operations go to primary"
if model._meta.app_label == 'django_cache':
return 'cache_primary'
return None
所有DB缓存的写操作指向 cache_primary DB
3.Multiple Databases——migration
def allow_migrate(self, db, app_label, model_name=None, **hints):
"Only install the cache model on primary"
if app_label == 'django_cache':
return db == 'cache_primary'
return None
允许在 cache_primary DB 上执行 migration
原文链接:http://www.maiziedu.com/wiki/django/database/
相关文章推荐
- django缓存优化中caches参数如何配置?
- django缓存优化中caches参数如何配置?
- 如何重新配置Oracle的EM Database Control
- 如何给企业配置可升级 Web 缓存方案
- 如何配置基于django的独立运行进程
- 如何在Django中正确的使用和设置Database和Model
- 手把手教你:Ubuntu+django+apache下配置网页错误OperationalError: Unable to Open Database File 的解决方法
- 在SharePoint中如何配置基于硬盘的缓存
- 如何优化tomcat配置(从内存、并发、缓存4个方面)优化
- Django——如何处理请求(URL配置和视图)
- 如何安装配置Django环境 (转)
- Windows下Magento企业版如何配置Redis存储缓存
- 数据库连接缓存概念和如何配置连接缓存
- 如何配置ThinkPHP中静态缓存的存放路径
- Django安装配置Memcached缓存优化性能
- 如何解决django配置settings时遇到Could not import settings 'conf.local'
- 如何重新配置Oracle的EM Database Control
- 如何配置ehcache 缓存指定的请求或jsp
- 如何配置重做日志高速缓存的大小
- hibernate 如何配置二级缓存