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缓存?的更多相关文章

  1. Redis缓存在django中的配置

    django  settings中的配置 # 缓存 CACHES = { "default": { "BACKEND": "django_redis. ...

  2. django中路由配置的正则

    在django中配置路由遇到正则的坑: django2.x版本中使用re_path来进行正则表达式的匹配 用法如下: from Django.urls import re.path(导入re_path ...

  3. django中日志配置

    # ======日志配置====== # 错误优先级:NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL # Djang ...

  4. Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)

    一.Django中的缓存: 前戏: 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一 ...

  5. django中的缓存以及跨域

    django中的缓存 先来了解以下问题

  6. 在django中使用Redis存取session

    一.Redis的配置 1.django的缓存配置 # redis在django中的配置 CACHES = { "default": { "BACKEND": & ...

  7. Django中的分页,cookies与session

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  8. Django中cookie&session的实现

    1.什么叫Cookie Cookie翻译成中文是小甜点,小饼干的意思.在HTTP中它表示服务器送给客户端浏览器的小甜点.其实Cookie是key-value结构,类似于一个python中的字典.随着服 ...

  9. Django中配置用Redis做缓存和session

    django-redis文档: http://django-redis-chs.readthedocs.io/zh_CN/latest/# 一.在Django中配置 # Django的缓存配置 CAC ...

随机推荐

  1. JS 滚动效果

    地址: https://github.com/aamirafridi/jQuery.Marquee <script language="JavaScript" src=&qu ...

  2. hnu Counting ones 统计1-n 二进制中1的个数

    Counting ones Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users ...

  3. hdu 1452 Happy 2004 膜拜这推导过程

    Happy 2004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 提高开发效率的 Eclipse 实用操作

    工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...

  5. ecshop商品分类页获取相册列表方法

    第1步:找到根目录的category.php文件,查找约:486行左右(注意这不是准确位置,看实际的哦),找到这个函数: /** * 获得分类下的商品 * * @access public * @pa ...

  6. HTTP缓存机制

    缓存对于移动端是非常重要的存在. 减少请求次数,减小服务器压力. 本地数据读取速度更快,让页面不会空白几百毫秒. 在无网络的情况下提供数据. 缓存一般由服务器控制(通过某些方式可以本地控制缓存,比如向 ...

  7. 【MySQL】MySQL的find_in_set的使用例子

    > 参考的优秀文章 FIND_IN_SET(str,strlist) > 简单的例子 这个函数的功能是,在第二个参数中寻找第一个参数,并返回第一个参数所在的位置,不存在则返回0.其中,第二 ...

  8. Random类和ThreadLocalRandom类

    Random类和ThreadLocalRandom类 Random类用于生成一个伪随机数,他有两个构造方法:一个构造方法使用默认的种子(以当前时间作为种子),另一个构造方法需要显示传入一个long型整 ...

  9. .Net验证码实现基础--Draw

    命名空间 using System.Draw; using System.Draw.Drawing2D; 在form等控件的 事件中 添加 paint事件 ///////画各种形状(空心)////// ...

  10. [转]Unity 延迟执行一段代码的较为优雅的方式

    Unity中,延时执行一段代码或者一个方法或者几个方法的情况非常普遍. 一般会用到Invoke和InvokeRepeating方法.顾名思义,第一个是执行一次,第二个是重复执行. 看下定义: void ...