Fixed #17256 -- Ensured that content types get cached when retrieved by natural key. Thanks, defaultwombat and charettes.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17502 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Julien Phalip 2012-02-11 09:55:54 +00:00
parent 995f7a16a8
commit 130e7ab617
3 changed files with 18 additions and 8 deletions

View File

@ -13,6 +13,7 @@ class ContentTypeManager(models.Manager):
ct = self.__class__._cache[self.db][(app_label, model)] ct = self.__class__._cache[self.db][(app_label, model)]
except KeyError: except KeyError:
ct = self.get(app_label=app_label, model=model) ct = self.get(app_label=app_label, model=model)
self._add_to_cache(self.db, ct)
return ct return ct
def _get_opts(self, model): def _get_opts(self, model):

View File

@ -51,8 +51,8 @@ class ContentTypesTests(TestCase):
def test_lookup_cache(self): def test_lookup_cache(self):
""" """
Make sure that the content type cache (see ContentTypeManager) Make sure that the content type cache (see ContentTypeManager)
works correctly. Lookups for a particular content type -- by model or works correctly. Lookups for a particular content type -- by model, ID
by ID -- should hit the database only on the first lookup. or natural key -- should hit the database only on the first lookup.
""" """
# At this point, a lookup for a ContentType should hit the DB # At this point, a lookup for a ContentType should hit the DB
@ -60,16 +60,30 @@ class ContentTypesTests(TestCase):
ContentType.objects.get_for_model(ContentType) ContentType.objects.get_for_model(ContentType)
# A second hit, though, won't hit the DB, nor will a lookup by ID # A second hit, though, won't hit the DB, nor will a lookup by ID
# or natural key
with self.assertNumQueries(0): with self.assertNumQueries(0):
ct = ContentType.objects.get_for_model(ContentType) ct = ContentType.objects.get_for_model(ContentType)
with self.assertNumQueries(0): with self.assertNumQueries(0):
ContentType.objects.get_for_id(ct.id) ContentType.objects.get_for_id(ct.id)
with self.assertNumQueries(0):
ContentType.objects.get_by_natural_key('contenttypes',
'contenttype')
# Once we clear the cache, another lookup will again hit the DB # Once we clear the cache, another lookup will again hit the DB
ContentType.objects.clear_cache() ContentType.objects.clear_cache()
with self.assertNumQueries(1): with self.assertNumQueries(1):
ContentType.objects.get_for_model(ContentType) ContentType.objects.get_for_model(ContentType)
# The same should happen with a lookup by natural key
ContentType.objects.clear_cache()
with self.assertNumQueries(1):
ContentType.objects.get_by_natural_key('contenttypes',
'contenttype')
# And a second hit shouldn't hit the DB
with self.assertNumQueries(0):
ContentType.objects.get_by_natural_key('contenttypes',
'contenttype')
def test_get_for_models_empty_cache(self): def test_get_for_models_empty_cache(self):
# Empty cache. # Empty cache.
with self.assertNumQueries(1): with self.assertNumQueries(1):

View File

@ -130,8 +130,7 @@ class CommentTemplateTagTests(CommentTestCase):
with self.assertNumQueries(4): with self.assertNumQueries(4):
self.testRenderCommentListFromObject() self.testRenderCommentListFromObject()
# Force the CT to be cached # CT's should be cached
ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(3): with self.assertNumQueries(3):
self.testRenderCommentListFromObject() self.testRenderCommentListFromObject()
@ -141,7 +140,6 @@ class CommentTemplateTagTests(CommentTestCase):
with self.assertNumQueries(4): with self.assertNumQueries(4):
self.verifyGetCommentList() self.verifyGetCommentList()
ct = ContentType.objects.get_for_model(Author)
with self.assertNumQueries(3): with self.assertNumQueries(3):
self.verifyGetCommentList() self.verifyGetCommentList()
@ -151,7 +149,6 @@ class CommentTemplateTagTests(CommentTestCase):
with self.assertNumQueries(3): with self.assertNumQueries(3):
self.testRenderCommentForm() self.testRenderCommentForm()
ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(2): with self.assertNumQueries(2):
self.testRenderCommentForm() self.testRenderCommentForm()
@ -161,7 +158,6 @@ class CommentTemplateTagTests(CommentTestCase):
with self.assertNumQueries(3): with self.assertNumQueries(3):
self.testGetCommentForm() self.testGetCommentForm()
ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(2): with self.assertNumQueries(2):
self.testGetCommentForm() self.testGetCommentForm()
@ -171,6 +167,5 @@ class CommentTemplateTagTests(CommentTestCase):
with self.assertNumQueries(3): with self.assertNumQueries(3):
self.verifyGetCommentCount() self.verifyGetCommentCount()
ct = ContentType.objects.get_for_model(Article)
with self.assertNumQueries(2): with self.assertNumQueries(2):
self.verifyGetCommentCount() self.verifyGetCommentCount()