From 63075118a528d1abf0b146a961ec5c571bf058b2 Mon Sep 17 00:00:00 2001
From: Mark Haines <mark.haines@matrix.org>
Date: Tue, 5 May 2015 16:24:04 +0100
Subject: [PATCH] Add debug flag in synapse/storage/_base.py for debugging the
 cache logic by comparing what is in the cache with what was in the database
 on every access

---
 synapse/storage/_base.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index 7f5477dee5..840a4994bb 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -33,6 +33,7 @@ import sys
 import time
 import threading
 
+DEBUG_CACHES = False
 
 logger = logging.getLogger(__name__)
 
@@ -146,7 +147,17 @@ def cached(max_entries=1000, num_args=1, lru=False):
         @defer.inlineCallbacks
         def wrapped(self, *keyargs):
             try:
-                defer.returnValue(cache.get(*keyargs))
+                cached_result = cache.get(*keyargs)
+                if DEBUG_CACHES:
+                    actual_result = yield orig(self, *keyargs)
+                    if actual_result != cached_result:
+                        logger.error(
+                            "Stale cache entry %s%r: cached: %r, actual %r",
+                            orig.__name__, keyargs,
+                            cached_result, actual_result,
+                        )
+                        raise ValueError("Stale cache entry")
+                defer.returnValue(cached_result)
             except KeyError:
                 sequence = cache.sequence
 
-- 
GitLab