From: Stephen Burrows Date: Fri, 22 Jul 2011 15:03:22 +0000 (-0400) Subject: Tweaked AttributeMapper._fill_cache to also store values on the Attribute instance... X-Git-Tag: philo-0.9.1^2~3^2~1^2 X-Git-Url: http://git.ithinksw.org/philo.git/commitdiff_plain/d527f10933dcaad1d2380c7ba471f61f46cd5feb?hp=-c Tweaked AttributeMapper._fill_cache to also store values on the Attribute instance itself. Made everything about AttributeMapper._fill_cache lazier. --- d527f10933dcaad1d2380c7ba471f61f46cd5feb diff --git a/philo/utils/entities.py b/philo/utils/entities.py index c7b14b7..830276e 100644 --- a/philo/utils/entities.py +++ b/philo/utils/entities.py @@ -89,18 +89,21 @@ class AttributeMapper(object, DictMixin): value_lookups.setdefault(a.value_content_type_id, []).append(a.value_object_id) self._attributes_cache[a.key] = a - values_bulk = {} + values_bulk = dict(((ct_pk, SimpleLazyObject(partial(ContentType.objects.get_for_id(ct_pk).model_class().objects.in_bulk, pks))) for ct_pk, pks in value_lookups.items())) - for ct_pk, pks in value_lookups.items(): - values_bulk[ct_pk] = ContentType.objects.get_for_id(ct_pk).model_class().objects.in_bulk(pks) + cache = {} - self._cache.update(dict([ - ( - a.key, - SimpleLazyObject(partial(getattr, values_bulk[a.value_content_type_id].get(a.value_object_id), 'value', None)) - ) for a in attributes])) + for a in attributes: + cache[a.key] = SimpleLazyObject(partial(self._lazy_value_from_bulk, values_bulk, a)) + a._value_cache = cache[a.key] + + self._cache.update(cache) self._cache_filled = True + def _lazy_value_from_bulk(self, bulk, attribute): + v = bulk[attribute.value_content_type_id].get(attribute.value_object_id) + return getattr(v, 'value', None) + def clear_cache(self): """Clears the cache.""" self._cache = {}