Django中如何配置Database缓存?
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中如何配置Database缓存?的更多相关文章
- Redis缓存在django中的配置
django settings中的配置 # 缓存 CACHES = { "default": { "BACKEND": "django_redis. ...
- django中路由配置的正则
在django中配置路由遇到正则的坑: django2.x版本中使用re_path来进行正则表达式的匹配 用法如下: from Django.urls import re.path(导入re_path ...
- django中日志配置
# ======日志配置====== # 错误优先级:NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL # Djang ...
- Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)
一.Django中的缓存: 前戏: 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一 ...
- django中的缓存以及跨域
django中的缓存 先来了解以下问题
- 在django中使用Redis存取session
一.Redis的配置 1.django的缓存配置 # redis在django中的配置 CACHES = { "default": { "BACKEND": & ...
- Django中的分页,cookies与session
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- Django中cookie&session的实现
1.什么叫Cookie Cookie翻译成中文是小甜点,小饼干的意思.在HTTP中它表示服务器送给客户端浏览器的小甜点.其实Cookie是key-value结构,类似于一个python中的字典.随着服 ...
- Django中配置用Redis做缓存和session
django-redis文档: http://django-redis-chs.readthedocs.io/zh_CN/latest/# 一.在Django中配置 # Django的缓存配置 CAC ...
随机推荐
- Uva 10003,切木棍
题目链接:https://uva.onlinejudge.org/external/100/10003.pdf 题意: L长的木棍,给n个切割点,切成n+1部分,每次切割的时候的费用等于切割时的长度. ...
- session和cookie的总结
cookie在客户端保持,而session在服务器端保持. 1.cookie机制: 产生:服务器通过http协议的响应头,指示浏览器产生相应的cookie信息 使用:浏览器按照一定规则通过ht ...
- ContentProvider官方教程(1)何时用content provider
Content Providers Content providers manage access to a structured set of data. They encapsulate the ...
- pt-query-digest怎么分析慢查询日志分析数据
在进行使用linux系统作为服务器的情况,那几需要进行对linux的服务器进行性能上数据进行抓取之后,就需要对数据中内容进行分析,看数据库中内容是否存在瓶颈上的问题,在进行获取到的数据的慢查日志,将使 ...
- Python3基础 if-else实例 判断输入的数字是否为8
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- VS2015使用技巧 打开代码片段C#部分
镇场诗: 大梦谁觉,水月中建博客.百千磨难,才知世事无常. 今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1. ...
- 如何在安装32位Oracle客户端组件的情况下以64位模式运行
C#使用System.Data.OracleClient连接Oracle数据库.之前在WinXP上正常运行的程序移植到Windows 2008 x64上之后就连不上数据库了,错误信息如下:启动data ...
- C# 多线程之一:信号量Semaphore
通过使用一个计数器对共享资源进行访问控制,Semaphore构造器需要提供初始化的计数器(信号量)大小以及最大的计数器大小 访问共享资源时,程序首先申请一个向Semaphore申请一个许可证,Sema ...
- JQuery多媒体插件jQuery Media Plugin使用详解
malsup jquery media plugin 该插件可以播放多种类型的多媒体文件包括:Flash, Quicktime, Windows Media Player, Real Player, ...
- Java_你应该知道的26种设计模式
四. 模板方法模式 Definition: Define the skeleton of an algorithm in an operation, deferring some steps to s ...