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
相关推荐
缓存对各位学习或者使用django的朋友们来说应该都不陌生,下面这篇文章主要给大家介绍了关于django缓存配置的几种方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
django-cacheback, 使用 celery 来异步刷新缓存项的Django的智能缓存 Cacheback针对 Django的异步缓存的异步缓存刷新:这个库做什么?它是一个可扩展缓存库,可以使用 celery 或者任务asynchronously缓存陈旧缓存项...
django-redis-cache, 面向 Django的Redis缓存后端 Django 缓存后端 面向 Django的Redis缓存后端可以在 http://django-redis-cache.readthedocs.org/en/latest/ 找到文档。变更日志 1.7.1确认
资源来自pypi官网。 资源全名:django-binary-database-files-1.0.12.tar.gz
django高级应用(url和view优化)
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5分钟内再有人来访问时...
在请求中,从db中检索对象,并通过django-sql-caching缓存在内存中; 稍后,在同一请求中,将修改该对象并将其保存回数据库; 再次在相同的请求中,您尝试使用相同的查询检索相同的对象; 缓存的,现在已经过时...
缓存就是为了防止重复计算,把那些消耗了大量资源的结果保存起来,下次访问时就不用再次计算了。缓存的逻辑: given a URL, try finding that page in the cache if the page is in the cache: return the cached ...
主要介绍了全面了解django的缓存机制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
g缓存Wagtail的快速简单页面缓存,受Django缓存中间件启发。 | |状态Python包 建立快速开始遵循为什么要使用Wagtail缓存? Django具有强大的缓存中间件,该中间件已经具有有效缓存网页所需的功能。 但是,打开缓存...
django模型的简单缓存管理器,用于缓存模型的查询集。 缓存管理器将缓存已针对模型看到的所有查询。 对于模型的任何更新/删除,都会驱逐模型缓存。 对于不经常更改的模型,此管理器很有用。 安装 pip install ...
YPlan/django-ratelimit: 为 Django 提供基于缓存的限速-Django Ratelimit 提供修饰器来声明限制, 可基于 IP 或请求, 无论 GET/POST 方法
django4最新中文文档+适合python初学或者初次接触django4的开发者 从事Python编程工作的人员,一定听说...Django内置了一个缓存框架,并提供了多种可选的缓存方式。 丰富的模板语言。Django模板语言功能丰富,支持自定
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存。 缓存工作原理:缓存是将一些常用的数据保存内存或者memcache中,在一定的时间...
Django实现内容缓存: 1、内存缓存 settings.py文件配置 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', # 指定缓存使用的引擎 'LOCATION': 'unique-snowflake', # 写在...
Django客户管理系统源码 Django客户管理系统源码 Django客户管理系统源码 Django客户管理系统源码 Django客户管理系统源码 Django客户管理系统源码 Django客户管理系统源码 Django客户管理系统...
django电子商务网站源码 django电子商务网站源码 django电子商务网站源码 django电子商务网站源码 django电子商务网站源码 django电子商务网站源码 django电子商务网站源码 django电子商务网站源码 django...
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者Redis中,5分钟内再有人来访问时,...
安装pip install django-fake-database-backends用法在项目设置文件中,大概是settings.py ,请使用可用的后端之一: DATABASES = { 'default': { 'ENGINE': 'django_fake_database_backends.backends.mysql', }}...