Is there a way to be sure that a page is coming from cache on a production server and on the development server as well?
The solution shouldn't involve caching middleware because not every project uses them. Though the solution itself might be a middleware.
Just checking if the data is stale is not a very safe testing method IMO.
If cache. get() returns the set value it means that cache is working as it should. Otherwise it will return None . An other option is to start memcached with $ memcached -vv , since it will log all the cache accesses to the terminal.
For convenience, Django offers different levels of cache granularity: You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site. Django also works well with “downstream” caches, such as Squid and browser-based caches.
Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.
Using Memcached One of the most popular and efficient types of cache supported natively by Django is MEMCACHED . As the name suggest MEMCACHED is a memory based cache server. It can dramatically reduce the number of databases queries a server has to do and increase the performance of an application by 10x.
We do a lot of component caching and not all of them are updated at the same time. So we set host and timestamp values in a universally included context processor. At the top of each template fragment we stick in:
<!-- component_name {{host}} {{timestamp}} -->
The component_name just makes it easy to do a View Source and search for that string.
All of our views that are object-detail pages define a context variable "page_object" and we have this at the top of the base.html template master:
<!-- {{page_object.class_id}} @ {{timestamp}} -->
class_id() is a method from a super class used by all of our primary content classes. It is just:
def class_id(self):
"%s.%s.%s" % (self.__class__._meta.app_label,
self.__class__.__name__, self.id)
If you load a page and any of the timestamps are more than few seconds old, it's a pretty good bet that the component was cached.
Peter Rowells suggestion works well, but you don't need a custom template context processor for timestamps. You can simply use the template tag:
<!-- {% now "jS F Y H:i" %} -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With