incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [47/50] git commit: [#4691] Fixed ModelCache potentially expiring instances too soon
Date Thu, 10 Jan 2013 18:08:21 GMT
[#4691] Fixed ModelCache potentially expiring instances too soon

Signed-off-by: Cory Johns <johnsca@geek.net>


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 <johnsca@geek.net>
Authored: Thu Dec 13 15:52:12 2012 +0000
Committer: Cory Johns <johnsca@geek.net>
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,
+            })


Mime
View raw message