From c428eb72a5b1574d3079c2ee8aff6cfd7e8d97fe Mon Sep 17 00:00:00 2001 From: Griatch Date: Mon, 20 Jan 2020 19:14:50 +0100 Subject: [PATCH] Have tags/attrs.all() switch to direct query on AGGRESSIVE_TYPECLASS_CACHE=False. Resolves #2007 --- evennia/typeclasses/attributes.py | 24 ++++++++++++++++-------- evennia/typeclasses/tags.py | 24 ++++++++++++++++-------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/evennia/typeclasses/attributes.py b/evennia/typeclasses/attributes.py index 2c66e4216f..2cb7ac8843 100644 --- a/evennia/typeclasses/attributes.py +++ b/evennia/typeclasses/attributes.py @@ -235,19 +235,23 @@ class AttributeHandler(object): # full cache was run on all attributes self._cache_complete = False - def _fullcache(self): - """Cache all attributes of this object""" - if not _TYPECLASS_AGGRESSIVE_CACHE: - return + def _query_all(self): + "Fetch all Attributes on this object" query = { "%s__id" % self._model: self._objid, "attribute__db_model__iexact": self._model, "attribute__db_attrtype": self._attrtype, } - attrs = [ + return [ conn.attribute 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( ( "%s-%s" @@ -776,9 +780,13 @@ class AttributeHandler(object): their values!) in the handler. """ - if not self._cache_complete: - self._fullcache() - attrs = sorted([attr for attr in self._cache.values() if attr], key=lambda o: o.id) + if _TYPECLASS_AGGRESSIVE_CACHE: + if not self._cache_complete: + self._fullcache() + 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: return [ attr diff --git a/evennia/typeclasses/tags.py b/evennia/typeclasses/tags.py index b93cb38428..2cd4ae027d 100644 --- a/evennia/typeclasses/tags.py +++ b/evennia/typeclasses/tags.py @@ -124,19 +124,23 @@ class TagHandler(object): # full cache was run on all tags self._cache_complete = False - def _fullcache(self): - "Cache all tags of this object" - if not _TYPECLASS_AGGRESSIVE_CACHE: - return + def _query_all(self): + "Get all tags for this objects" query = { "%s__id" % self._model: self._objid, "tag__db_model": self._model, "tag__db_tagtype": self._tagtype, } - tags = [ + return [ conn.tag 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( ( "%s-%s" @@ -425,9 +429,13 @@ class TagHandler(object): `return_key_and_category` is set. """ - if not self._cache_complete: - self._fullcache() - tags = sorted(self._cache.values()) + if _TYPECLASS_AGGRESSIVE_CACHE: + if not self._cache_complete: + self._fullcache() + tags = sorted(self._cache.values()) + else: + tags = sorted(self._query_all()) + if return_key_and_category: # return tuple (key, category) return [(to_str(tag.db_key), tag.db_category) for tag in tags]