Django cache framework. What is the difference between TIMEOUT and CACHE_MIDDLEWARE_SECONDS?

I set up caching in Django using the database cache. There are two parameters, TIMEOUT and CACHE_MIDDLEWARE_SECONDS, which determine how long the page caches. What is the difference between these two settings?

+8
source share
3 answers

In fact, the relevant documentation does not provide an adequate explanation of the differences.

The first CACHES : TIMEOUT option is represented in the Django cache structure, cache arguments . This is the default expiration time used by functions such as cache.set() , unless otherwise specified. This is later described in the low-level use of the API cache .

The latter, CACHE_MIDDLEWARE_SECONDS is represented in the Django cache structure, a cache for each site . Therefore, we can safely assume that this is the default expiration time for all pages, as if @cache_page(settings.CACHE_MIDDLEWARE_SECONDS) would be used for per-view .

+4
source

I had the same question, and the existing answers still did not clarify me. So I decided to dive into the source code. Yay for open source!

CACHE_MIDDLEWARE_SECONDS used by the UpdateCacheMiddleware . It sets the Cache-Control ( max-age ) CACHE_MIDDLEWARE_SECONDS to CACHE_MIDDLEWARE_SECONDS if the view has not set it yet, which affects the client-side cache. Here is the code:

  self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS # ... timeout = get_max_age(response) if timeout is None: timeout = self.cache_timeout patch_response_headers(response, timeout) 

(Please note that I cut out the code and edge angles to make it faster to read, you can of course read the full source code).

It also stores the response in the server-side cache, using the same timeout value that comes from MIDDLEWARE_CACHE_SECONDS , overriding the TIMEOUT parameter if it was set: ( context )

  if timeout: cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache) self.cache.set(cache_key, response, timeout) 

The middleware tool is FetchFromCacheMiddleware with UpdateCacheMiddleware and uses the server-side cache values ​​set by the latter, therefore it is indirectly affected by CACHE_MIDDLEWARE_SECONDS .

Alternative middleware CacheMiddleware also uses CACHE_MIDDLEWARE_SECONDS . This should not affect you if you are not using CacheMiddleware .

So what is the TIMEOUT installation point? I suppose this is the default value that is used if you write directly to the cache, but it is not used by the previously mentioned middleware. For instance:

 from django.core.cache import cache cache.set('my_key', 'my_value') # uses TIMEOUT value as default 
+4
source

According to http://www.djangobook.com/en/2.0/chapter15.html , TIMEOUT is the timeout for connecting to the cache server, and CACHE_MIDDLEWARE_SECONDS is the number of seconds to cache the page. Therefore, TIMEOUT is not necessarily useful for all backends.

0
source

Source: https://habr.com/ru/post/976274/


All Articles