From 286e59074dbd51251c108655d53a239d29280558 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Sat, 19 Dec 2015 02:58:38 +0100 Subject: [PATCH] Documents how to use django-cachalot with a replica database. --- docs/limits.rst | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/limits.rst b/docs/limits.rst index 1080eca..265a567 100644 --- a/docs/limits.rst +++ b/docs/limits.rst @@ -117,3 +117,29 @@ A difference of even a few seconds can be harmful, so double check this! To keep your clocks synchronised, use the `Network Time Protocol `_. + +Replication server +.................. + +If you use multiple databases where at least one is a replica of another, +django-cachalot has no way to know that the replica is modified +automatically, since it happens outside Django. +The SQL queries cached for the replica will therefore not be invalidated, +and you will see some stale queries results. + +To fix this problem, you need to tell django-cachalot to also invalidate +the replica when the primary database is invalidated. +Suppose your primary database has the ``'default'`` database alias +in ``DATABASES``, and your replica has the ``'replica'`` alias. +Use :ref:`the signal ` and :meth:`cachalot.api.invalidate` this way: + +.. code:: python + + from cachalot.api import invalidate + from cachalot.signals import post_invalidation + from django.dispatch import receiver + + @receiver(post_invalidation) + def invalidate_replica(sender, **kwargs): + if kwargs['db_alias'] == 'default': + invalidate(sender, db_alias='replica')