Fixes situation where attributes and tags continue to be cached when TYPECLASS_AGGRESSIVE_CACHE is set to False.

This commit is contained in:
Johnny 2019-12-03 21:35:51 +00:00
parent a6f5cccdfa
commit 270b50a984
3 changed files with 39 additions and 16 deletions

View file

@ -237,6 +237,7 @@ class AttributeHandler(object):
def _fullcache(self): def _fullcache(self):
"""Cache all attributes of this object""" """Cache all attributes of this object"""
if not _TYPECLASS_AGGRESSIVE_CACHE: return
query = { query = {
"%s__id" % self._model: self._objid, "%s__id" % self._model: self._objid,
"attribute__db_model__iexact": self._model, "attribute__db_model__iexact": self._model,
@ -298,7 +299,7 @@ class AttributeHandler(object):
attr = None attr = None
cachefound = False cachefound = False
del self._cache[cachekey] del self._cache[cachekey]
if cachefound: if cachefound and _TYPECLASS_AGGRESSIVE_CACHE:
if attr: if attr:
return [attr] # return cached entity return [attr] # return cached entity
else: else:
@ -316,13 +317,15 @@ class AttributeHandler(object):
conn = getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query) conn = getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)
if conn: if conn:
attr = conn[0].attribute attr = conn[0].attribute
self._cache[cachekey] = attr if _TYPECLASS_AGGRESSIVE_CACHE:
self._cache[cachekey] = attr
return [attr] if attr.pk else [] return [attr] if attr.pk else []
else: else:
# There is no such attribute. We will explicitly save that # There is no such attribute. We will explicitly save that
# in our cache to avoid firing another query if we try to # in our cache to avoid firing another query if we try to
# retrieve that (non-existent) attribute again. # retrieve that (non-existent) attribute again.
self._cache[cachekey] = None if _TYPECLASS_AGGRESSIVE_CACHE:
self._cache[cachekey] = None
return [] return []
else: else:
# only category given (even if it's None) - we can't # only category given (even if it's None) - we can't
@ -345,12 +348,13 @@ class AttributeHandler(object):
**query **query
) )
] ]
for attr in attrs: if _TYPECLASS_AGGRESSIVE_CACHE:
if attr.pk: for attr in attrs:
cachekey = "%s-%s" % (attr.db_key, category) if attr.pk:
self._cache[cachekey] = attr cachekey = "%s-%s" % (attr.db_key, category)
# mark category cache as up-to-date self._cache[cachekey] = attr
self._catcache[catkey] = True # mark category cache as up-to-date
self._catcache[catkey] = True
return attrs return attrs
def _setcache(self, key, category, attr_obj): def _setcache(self, key, category, attr_obj):
@ -363,6 +367,7 @@ class AttributeHandler(object):
attr_obj (Attribute): The newly saved attribute attr_obj (Attribute): The newly saved attribute
""" """
if not _TYPECLASS_AGGRESSIVE_CACHE: return
if not key: # don't allow an empty key in cache if not key: # don't allow an empty key in cache
return return
cachekey = "%s-%s" % (key, category) cachekey = "%s-%s" % (key, category)

View file

@ -121,6 +121,7 @@ class TagHandler(object):
def _fullcache(self): def _fullcache(self):
"Cache all tags of this object" "Cache all tags of this object"
if not _TYPECLASS_AGGRESSIVE_CACHE: return
query = { query = {
"%s__id" % self._model: self._objid, "%s__id" % self._model: self._objid,
"tag__db_model": self._model, "tag__db_model": self._model,
@ -188,7 +189,8 @@ class TagHandler(object):
conn = getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query) conn = getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)
if conn: if conn:
tag = conn[0].tag tag = conn[0].tag
self._cache[cachekey] = tag if _TYPECLASS_AGGRESSIVE_CACHE:
self._cache[cachekey] = tag
return [tag] return [tag]
else: else:
# only category given (even if it's None) - we can't # only category given (even if it's None) - we can't
@ -211,11 +213,12 @@ class TagHandler(object):
**query **query
) )
] ]
for tag in tags: if _TYPECLASS_AGGRESSIVE_CACHE:
cachekey = "%s-%s" % (tag.db_key, category) for tag in tags:
self._cache[cachekey] = tag cachekey = "%s-%s" % (tag.db_key, category)
# mark category cache as up-to-date self._cache[cachekey] = tag
self._catcache[catkey] = True # mark category cache as up-to-date
self._catcache[catkey] = True
return tags return tags
return [] return []
@ -229,6 +232,7 @@ class TagHandler(object):
tag_obj (tag): The newly saved tag tag_obj (tag): The newly saved tag
""" """
if not _TYPECLASS_AGGRESSIVE_CACHE: return
if not key: # don't allow an empty key in cache if not key: # don't allow an empty key in cache
return return
key, category = key.strip().lower(), category.strip().lower() if category else category key, category = key.strip().lower(), category.strip().lower() if category else category

View file

@ -2,8 +2,9 @@
Unit tests for typeclass base system Unit tests for typeclass base system
""" """
from django.test import override_settings
from evennia.utils.test_resources import EvenniaTest from evennia.utils.test_resources import EvenniaTest
from mock import patch
# ------------------------------------------------------------ # ------------------------------------------------------------
# Manager tests # Manager tests
@ -19,6 +20,19 @@ class TestAttributes(EvenniaTest):
self.obj1.db.testattr = value self.obj1.db.testattr = value
self.assertEqual(self.obj1.db.testattr, value) self.assertEqual(self.obj1.db.testattr, value)
@override_settings(TYPECLASS_AGGRESSIVE_CACHE=False)
@patch('evennia.typeclasses.attributes._TYPECLASS_AGGRESSIVE_CACHE', False)
def test_attrhandler_nocache(self):
key = "testattr"
value = "test attr value "
self.obj1.attributes.add(key, value)
self.assertFalse(self.obj1.attributes._cache)
self.assertEqual(self.obj1.attributes.get(key), value)
self.obj1.db.testattr = value
self.assertEqual(self.obj1.db.testattr, value)
self.assertFalse(self.obj1.attributes._cache)
def test_weird_text_save(self): def test_weird_text_save(self):
"test 'weird' text type (different in py2 vs py3)" "test 'weird' text type (different in py2 vs py3)"
from django.utils.safestring import SafeText from django.utils.safestring import SafeText