- if self._tag_counter is None:
- return self.refresh_tag_counter()
- return dict((int(k), v) for k, v in self.get__tag_counter_value().iteritems())
-
- def refresh_theme_counter(self):
- tags = {}
- for fragment in Fragment.tagged.with_any([self.book_tag()]).order_by():
- for tag in fragment.tags.filter(category='theme').order_by():
- tags[tag.pk] = tags.get(tag.pk, 0) + 1
- self.set__theme_counter_value(tags)
- self.save(reset_short_html=False)
+ cache_key = "Book.tag_counter/%d" % self.id
+ tags = cache.get(cache_key)
+ if tags is None:
+ tags = {}
+ for child in self.children.all().order_by():
+ for tag_pk, value in child.tag_counter.iteritems():
+ tags[tag_pk] = tags.get(tag_pk, 0) + value
+ for tag in self.tags.exclude(category__in=('book', 'theme', 'set')).order_by():
+ tags[tag.pk] = 1
+
+ cache.set(cache_key, tags)