Commits

Branko Vukelic committed f8d4718

Added caching to get_related_object method

This closes #1

  • Participants
  • Parent commits 4872415

Comments (0)

Files changed (1)

 from django.http import HttpResponseRedirect, HttpResponseGone, Http404
 from django.contrib import messages
 from django.db import IntegrityError
+from django.core.cache import cache
 
 
 class GetExistingMixin(object):
         if not object_kwargs:
             return None
 
-        return self.related_model.objects.get(**object_kwargs)
+        # Use object_kwargs as key
+        cache_key = ''
+        for k, v in object_kwargs.items():
+            cache_key = '%s%s' % (k, v)
+
+        cached = cache.get(cache_key)
+
+        if cached:
+            return cached
+
+        related_object = self.related_model.objects.get(**object_kwargs)
+        cache.set(cache_key, related_object, 30)
+        return related_object
 
     def get_integrity_error_message(self):
         return self.integrity_error_message