Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database Django缓存独立数据库_Database_Django_Caching - Fatal编程技术网

Database Django缓存独立数据库

Database Django缓存独立数据库,database,django,caching,Database,Django,Caching,我的django项目使用单独的数据库,该数据库必须使用django缓存进行缓存。因此,这条路并不直截了当 我读了进去,但我听不懂。该链接显示了一个带有一些def的“CacheRouter”模型 我不知道是不是 示例代码或 代码已存在于某个位置或其他位置 我必须修改或修改的代码 我必须在模型中添加的代码 有人能详细解释一下吗?你可以做一个通用路由器,它是使用进入非默认数据库的应用程序的dict配置的 app_to_database = { 'django_cache': 'the_de

我的django项目使用单独的数据库,该数据库必须使用django缓存进行缓存。因此,这条路并不直截了当

我读了进去,但我听不懂。该链接显示了一个带有一些def的“CacheRouter”模型

我不知道是不是 示例代码或

  • 代码已存在于某个位置或其他位置
  • 我必须修改或修改的代码
  • 我必须在模型中添加的代码

有人能详细解释一下吗?

你可以做一个通用路由器,它是使用进入非默认数据库的应用程序的
dict
配置的

app_to_database = {
    'django_cache': 'the_declared_name_of_the_cache_database',
}

class CacheRouter(object):

    def db_for_read(self, model, **hints):
        return app_to_database.get(model._meta.app_label, None)

    def db_for_write(self, model, **hints):
        return app_to_database.get(model._meta.app_label, None)

    def allow_syncdb(self, db, model):
        _db = app_to_database.get(model._meta.app_label, None)
        return db == _db if _db else None
编辑:更好的解释

如果您想使用不同的架构、数据库服务器甚至不同的网络后端,则需要在
settings.py
中声明它们:

DATABASES = {
    'default': # the one storing your app data
        { 'NAME': 'main_db', # this is the schema name
          'USER': '...',
          'PASSWORD': '...',
          'ENGINE': '...', # etc...
        },
    'the_declared_name_of_the_cache_database': # the name is arbitrary here
        { 'NAME': 'db_for_djcache', # this is the schema name
          'USER': '...',
          'PASSWORD': '...',
          'ENGINE': '...', # etc...
        },
}

DATABASE_ROUTERS = ['path.to.MyAppRouter'] # that is, the module.ClassName of the
                                           # router class you defined above.
另一方面,如果正如您在评论中所暗示的那样,您只需要一个不同的表(在同一个模式中),您根本不需要路由器,那么django可以顺利地管理一切


您可以使用任何GUI客户端检查数据库发生了什么。只需打开您的模式(ta)并检查表及其内容。

我不确定您在问什么问题或有什么问题……您能澄清一下吗?我正在手动选择单独的数据库,该数据库需要缓存。我需要写路由器吗?如果是,在哪里?模特?那么settings.py怎么知道呢?我相信我们使用一个单独的表来缓存。那么,我应该在“缓存数据库的名称”中输入什么?那么我是否需要在app.models.py中编写此代码?如果是这样的话,settings.py将如何知道这段代码?好的,如果我添加这个CacheRouter,我将如何知道它正在工作?如何检查?