Django 多个缓存后端路由器如何?
所以我想在mysql中缓存一些数据,在memcached中缓存一些数据。
目前我的配置文件中有这个,但我不知道如何为缓存后端编写路由器。
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
我使用多数据库结构,并且我知道如何编写多数据库路由器。
在settings.py中
DATABASE_ROUTERS = ['oceankeys.dbrouter.SphinxRouter','oceankeys.dbrouter.DefaultDbRouter']
有人知道如何制作Django缓存BACKEND路由器吗?
谢谢
So I want to cache some data in mysql and some in memcached.
at the moment I have this In my config file, but i don't know how to write router for cache back end.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
I use multi databases structure and I know how to write multi database routers.
in settings.py
DATABASE_ROUTERS = ['oceankeys.dbrouter.SphinxRouter','oceankeys.dbrouter.DefaultDbRouter']
Any one know how to make Django caching BACKEND router?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不相信 Django 缓存框架可以一般模仿数据库路由。
对于使用缓存框架的站点缓存 中间件你必须在settings.py中指定缓存的名称,例如:
对于页面缓存,你可以在装饰器中手动指定缓存的名称,例如:
我不确定缓存路由对于站点和页面缓存是否真的有意义,所以我对其设计方式没有问题。
现在,对于使用 MySQL 作为数据库缓存后端的情况,您可以根据 数据库缓存。例如,这将是您的
CACHES
设置:然后创建一个路由器来标识哪个缓存后端用于哪些模型。它的外观和工作方式与数据库路由器完全相同(正如您应该能够从 数据库缓存和多个数据库),但它返回缓存别名而不是数据库别名。
I don't believe the Django cache framework can mimic db routing in general.
For the site cache using the cache framework middleware you have to specify the name of the cache in settings.py, e.g.:
For a page cache you can manually specify the name of the cache in the decorator, e.g.:
I'm not sure cache routing really makes sense for site and page caching so I don't have a problem with the way this is designed.
Now, for your case where you are using MySQL as a database cache backend you can set it up and make a router as per the Django docs section on database caching. For example, this would be your
CACHES
setting:Then create a router that identifies which cache backend to use for which models. It looks and works exactly like DB router (as you should be able to see from the doc section on database caching and multiple databases) with the exception that it returns a cache alias instead of db alias.
eg
settings.py
views.py
您可以在 Django 的缓存框架(部分:
访问缓存
)e.g
settings.py
views.py
You can see the detail in Django’s cache framework (section:
Accessing the cache
)