Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8EF18EABD for ; Thu, 10 Jan 2013 18:08:21 +0000 (UTC) Received: (qmail 43010 invoked by uid 500); 10 Jan 2013 18:08:21 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 42967 invoked by uid 500); 10 Jan 2013 18:08:21 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 42907 invoked by uid 99); 10 Jan 2013 18:08:21 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Jan 2013 18:08:21 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 2AEFB17756; Thu, 10 Jan 2013 18:08:21 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: johnsca@apache.org To: allura-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [47/50] git commit: [#4691] Fixed ModelCache potentially expiring instances too soon Message-Id: <20130110180821.2AEFB17756@tyr.zones.apache.org> Date: Thu, 10 Jan 2013 18:08:21 +0000 (UTC) [#4691] Fixed ModelCache potentially expiring instances too soon Signed-off-by: Cory Johns Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/668b7f5f Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/668b7f5f Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/668b7f5f Branch: refs/heads/cj/4691 Commit: 668b7f5f6f6e863ce17ba5a899e4bc9775d8e2fc Parents: 4374a43 Author: Cory Johns Authored: Thu Dec 13 15:52:12 2012 +0000 Committer: Cory Johns Committed: Thu Jan 10 16:27:26 2013 +0000 ---------------------------------------------------------------------- Allura/allura/model/repo.py | 2 +- Allura/allura/tests/model/test_repo.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/668b7f5f/Allura/allura/model/repo.py ---------------------------------------------------------------------- diff --git a/Allura/allura/model/repo.py b/Allura/allura/model/repo.py index 6d8a61f..c517609 100644 --- a/Allura/allura/model/repo.py +++ b/Allura/allura/model/repo.py @@ -904,12 +904,12 @@ class ModelCache(object): def set(self, cls, query, val): _query = self._normalize_query(query) - self._touch(cls, _query) _id = self._query_cache[cls].get(_query, getattr(val, '_id', None)) if _id is None: _id = 'None_%s' % bson.ObjectId() self._query_cache[cls][_query] = _id self._instance_cache[cls][_id] = val + self._touch(cls, _query) self._check_sizes(cls) def _touch(self, cls, query): http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/668b7f5f/Allura/allura/tests/model/test_repo.py ---------------------------------------------------------------------- diff --git a/Allura/allura/tests/model/test_repo.py b/Allura/allura/tests/model/test_repo.py index 40af7ce..365251a 100644 --- a/Allura/allura/tests/model/test_repo.py +++ b/Allura/allura/tests/model/test_repo.py @@ -660,3 +660,24 @@ class TestModelCache(unittest.TestCase): 'foo': tree4, }, }) + + def test_pruning_query_vs_instance(self): + cache = M.repo.ModelCache(max_queries=3, max_instances=2) + # ensure cache expires as LRU + tree1 = mock.Mock(_id='keep', val='bar') + tree2 = mock.Mock(_id='tree2', val='fuz') + tree3 = mock.Mock(_id='tree3', val='b4r') + tree4 = mock.Mock(_id='tree4', val='zaz') + cache.set(M.repo.Tree, {'keep_query_1': 'bar'}, tree1) + cache.set(M.repo.Tree, {'drop_query_1': 'bar'}, tree2) + cache.set(M.repo.Tree, {'keep_query_2': 'bar'}, tree1) # should refresh tree1 in _instance_cache + cache.set(M.repo.Tree, {'drop_query_2': 'bar'}, tree3) # should drop tree2, not tree1, from _instance_cache + self.assertEqual(cache._query_cache[M.repo.Tree], { + (('drop_query_1', 'bar'),): 'tree2', + (('keep_query_2', 'bar'),): 'keep', + (('drop_query_2', 'bar'),): 'tree3', + }) + self.assertEqual(cache._instance_cache[M.repo.Tree], { + 'keep': tree1, + 'tree3': tree3, + })