You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by tv...@apache.org on 2013/02/05 21:23:35 UTC

[19/42] git commit: [#4691] Fixed ModelCache potentially expiring instances too soon

[#4691] Fixed ModelCache potentially expiring instances too soon

Signed-off-by: Cory Johns <jo...@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/d8e44d14
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/d8e44d14
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/d8e44d14

Branch: refs/heads/master
Commit: d8e44d1489eeb00c38ac7aeb3b13d786c78944d7
Parents: ada5e96
Author: Cory Johns <jo...@geek.net>
Authored: Thu Dec 13 15:52:12 2012 +0000
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Tue Feb 5 20:22:51 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/d8e44d14/Allura/allura/model/repo.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/repo.py b/Allura/allura/model/repo.py
index f1fe984..c92d403 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/d8e44d14/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,
+            })