1

According to Django recommendation UpdateCacheMiddleware should be at the beginning of the middlewares list while FetchFromCacheMiddleware should be at the end.

I was wondering, doesn't that mean that when I save a response to the cache, it will be AFTER is passed through all of the middlewares (in the request phase and then again in the response phase),

but when I fetch a response from the cache, it will be already AFTER it passed through all of the middlewares, and then it will go back AGAIN through all of the middlewares in the response phase?

Does it mean that all middlewares should be able to receive a response that they already processed?

user972014
  • 3,296
  • 6
  • 49
  • 89

2 Answers2

0

So there's two methods which apply to what you're talking about in middleware.

You have process_request and then process_response.

As you make a request, django goes through the middleware from the top, to the bottom calling process_request. Then it hits the view, and on the way back with the response the middleware is processed from the bottom to the top, calling process_response.

Specific to your examples with cache, FetchFromCacheMiddleware contains process_request and UpdateCacheMiddleware contains process_response.

To quote the docs...

Middleware order and layering

During the request phase, before calling the view, Django applies middleware in the order it’s defined in MIDDLEWARE, top-down.

You can think of it like an onion: each middleware class is a "layer" that wraps the view, which is in the core of the onion. If the request passes through all the layers of the onion (each one calls get_response to pass the request in to the next layer), all the way to the view at the core, the response will then pass through every layer (in reverse order) on the way back out.

If one of the layers decides to short-circuit and return a response without ever calling its get_response, none of the layers of the onion inside that layer (including the view) will see the request or the response. The response will only return through the same layers that the request passed in through.

The docs are here; https://docs.djangoproject.com/en/3.1/topics/http/middleware/#middleware-order-and-layering

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • I think maybe my question wasn't clear. UpdateCacheMiddleware will save to cache the response after it was processed by all of the middlewares in reponse phase. In the next call, Fetch is in the middle of the onion, it will retrieve from the cache this response, and then will return it, which will make all of the other middlewares to process it AGAIN in the response phase. No? – user972014 Sep 17 '20 at 14:50
0
Django middle ware works like a sandwich:

We have two main parts to run for each middleware

Request from user

UpdateCache.process_request (no much action here)
FetchCache.process_request (all action is here) ,The Cache can return a response here


actual Views

FetchCache.process_request (no much action here)
UpdateCache.process_request (the cache is updated with the fresh response here)


response to user

So UpdateCache is put at the first, to be run at the end of the cycle.

kerolos
  • 127
  • 1
  • 2
  • 10