Commits

Branko Vukelic committed d7b5c18

Customizable caching interface

  • Participants
  • Parent commits 7dd5ca2

Comments (0)

Files changed (2)

     to the response object using ``django.contrib.messages``. This attribute
     customizes the message. Default is ``'Such record already exists'``.
 
+``cache_backend`` (``get_cache_backend``)
+    Specifies the object that implements the caching methods. This object is
+    ``django.core.caching.cache`` by default. Any interface that you specify
+    must provide the same methods as the default one.
+
 Reporting bugs
 ==============
 

File related/views.py

     related_object_gone_message = '<h2>Database record is missing</h2>'
     related_object_name = None
     integrity_error_message = 'Such record already exists'
+    cache_backend = cache
 
     def get_related_404_url(self):
         return self.related_404_redirect_url
         for k, v in object_kwargs.items():
             cache_key = '%s%s' % (k, v)
 
-        cached = cache.get(cache_key)
+        cache_backend = self.get_cache_backend()
+        cached = cache_backend.get(cache_key)
 
         if cached:
             return cached
 
         related_object = self.related_model.objects.get(**object_kwargs)
-        cache.set(cache_key, related_object, 30)
+        cache_backend.set(cache_key, related_object, 30)
         return related_object
 
     def get_integrity_error_message(self):
     def get_related_object_name(self):
         return self.related_object_name
 
+    def get_cache_backend(self):
+        return self.cache_backend
+
     def form_valid(self, form):
         try:
             related_object = self.get_related_object()