Have tags/attrs.all() switch to direct query on AGGRESSIVE_TYPECLASS_CACHE=False. Resolves #2007

This commit is contained in:
Griatch 2020-01-20 19:14:50 +01:00
parent d806909f33
commit c428eb72a5
2 changed files with 32 additions and 16 deletions

View file

@ -235,19 +235,23 @@ class AttributeHandler(object):
# full cache was run on all attributes # full cache was run on all attributes
self._cache_complete = False self._cache_complete = False
def _fullcache(self): def _query_all(self):
"""Cache all attributes of this object""" "Fetch all Attributes on 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,
"attribute__db_attrtype": self._attrtype, "attribute__db_attrtype": self._attrtype,
} }
attrs = [ return [
conn.attribute conn.attribute
for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query) for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)
] ]
def _fullcache(self):
"""Cache all attributes of this object"""
if not _TYPECLASS_AGGRESSIVE_CACHE:
return
attrs = self._query_all()
self._cache = dict( self._cache = dict(
( (
"%s-%s" "%s-%s"
@ -776,9 +780,13 @@ class AttributeHandler(object):
their values!) in the handler. their values!) in the handler.
""" """
if _TYPECLASS_AGGRESSIVE_CACHE:
if not self._cache_complete: if not self._cache_complete:
self._fullcache() self._fullcache()
attrs = sorted([attr for attr in self._cache.values() if attr], key=lambda o: o.id) attrs = sorted([attr for attr in self._cache.values() if attr], key=lambda o: o.id)
else:
attrs = sorted([attr for attr in self._query_all() if attr], key=lambda o: o.id)
if accessing_obj: if accessing_obj:
return [ return [
attr attr

View file

@ -124,19 +124,23 @@ class TagHandler(object):
# full cache was run on all tags # full cache was run on all tags
self._cache_complete = False self._cache_complete = False
def _fullcache(self): def _query_all(self):
"Cache all tags of this object" "Get all tags for this objects"
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,
"tag__db_tagtype": self._tagtype, "tag__db_tagtype": self._tagtype,
} }
tags = [ return [
conn.tag conn.tag
for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query) for conn in getattr(self.obj, self._m2m_fieldname).through.objects.filter(**query)
] ]
def _fullcache(self):
"Cache all tags of this object"
if not _TYPECLASS_AGGRESSIVE_CACHE:
return
tags = self._query_all()
self._cache = dict( self._cache = dict(
( (
"%s-%s" "%s-%s"
@ -425,9 +429,13 @@ class TagHandler(object):
`return_key_and_category` is set. `return_key_and_category` is set.
""" """
if _TYPECLASS_AGGRESSIVE_CACHE:
if not self._cache_complete: if not self._cache_complete:
self._fullcache() self._fullcache()
tags = sorted(self._cache.values()) tags = sorted(self._cache.values())
else:
tags = sorted(self._query_all())
if return_key_and_category: if return_key_and_category:
# return tuple (key, category) # return tuple (key, category)
return [(to_str(tag.db_key), tag.db_category) for tag in tags] return [(to_str(tag.db_key), tag.db_category) for tag in tags]