Commits

Seraphim Mellos  committed 8bf8373

Additional work on caching

  • Participants
  • Parent commits 6a15571

Comments (0)

Files changed (3)

File transifex/resources/handlers.py

 
 def on_ss_save_invalidate_cache(sender, instance, created, **kwargs):
     """Invalidate cache keys related to the SourceEntity updates"""
+    invalidate_object_cache(instance.resource)
+
+    invalidate_object_cache(instance.resource.project)
+
+    for rel in instance.resource.project.releases.all():
+        invalidate_object_cache(rel)
 
     if created:
-        invalidate_object_cache(instance.resource)
         # Number of source strings in resource
         for lang in instance.resource.available_languages:
             team = Team.objects.get_or_none(instance.resource.project, lang.code)
     """Invalidate cache keys related to the SourceEntity updates"""
     if instance and instance.resource and instance.resource.project:
         invalidate_object_cache(instance.resource)
+        invalidate_object_cache(instance.resource.project)
+
+        for rel in instance.resource.project.releases.all():
+            invalidate_object_cache(rel)
 
         for lang in instance.resource.available_languages:
             team = Team.objects.get_or_none(instance.resource.project, lang.code)
      - Invalidate the translated_strings for this resource/language
      - Invalidate the last_updated property for this language
     """
+    invalidate_object_cache(instance.source_entity.resource,
+        instance.language)
+    invalidate_object_cache(instance.source_entity.resource.project, instance.language)
+
+    for rel in instance.source_entity.resource.project.releases.all():
+        invalidate_object_cache(rel, instance.language)
 
     if created:
-        invalidate_object_cache(instance.source_entity.resource)
-
         team = Team.objects.get_or_none(instance.source_entity.resource.project,
             instance.language.code)
         if team:
      - Invalidate the translated_stings for the resource/language
      - Invalidate the last_updated property for this language
     """
-    invalidate_object_cache(instance.source_entity.resource)
+    invalidate_object_cache(instance.source_entity.resource, instance.language)
+    invalidate_object_cache(instance.source_entity.resource.project, instance.language)
+
+    for rel in instance.source_entity.resource.project.releases.all():
+        invalidate_object_cache(rel, instance.language)
+
+    # Invalidate resource details template cache for this lang
+    invalidate_template_cache("resource_details",
+        instance.source_entity.resource.project.slug,
+        instance.source_entity.resource.slug, instance.language.code)
 
     team = Team.objects.get_or_none(instance.source_entity.resource.project,
         instance.language.code)

File transifex/resources/stats.py

         Useful to render a table of available languages and their statistics.
         """
         for language in self.available_languages:
-            yield self.stat(language=language)
+            sa = self.stat(language=language)
+            sa.object = self.object
+            yield sa
 
     def resource_stats(self):
         """Yield a Stat object for each available resource.

File transifex/resources/utils.py

     def cached_func(self):
         if not self.object:
             return func(self)
-        key = 'cached_property_%s_%s_%s' % \
-            (key_from_instance(self.object), func.__module__, func.__name__)
+
+        if hasattr(self, "language") and self.language:
+            key = 'cached_property_%s_%s_%s_%s' % \
+                (key_from_instance(self.object), func.__module__,
+                func.__name__, self.language.code )
+        else:
+            key = 'cached_property_%s_%s_%s' % \
+                (key_from_instance(self.object), func.__module__, func.__name__)
         return cache.get(key) or cache_set(key, func(self))
     return property(cached_func)
 
-def invalidate_object_cache(object):
+def invalidate_object_cache(object, language=None):
     """
     Invalidate all cached properties of a specific object's stats. The only
     properties that are actually invalidated are those who belong to a Stats
     from resources.stats import Stats, StatsBase
 
     keys = []
-    for c in [Stats, StatsBase]:
+    for name, type in inspect.getmembers(StatsBase):
+        if type.__class__.__name__ == "property":
+            keys.append("_".join([Stats.__module__, name]))
+
+    if language:
         for name, type in inspect.getmembers(Stats):
             if type.__class__.__name__ == "property":
-                keys.append("_".join([c.__module__, name]))
+                keys.append("_".join([StatsBase.__module__, name, language.code]))
 
     for key in keys:
         cache.delete("cached_property_%s_%s" % (key_from_instance(object), key))