Django caching error .. even if caching is disabled

I have a Django site where a strange error occurs.

On the site, they can add "publications", which basically coincides with a blog entry under a different name.

Things become strange when they modify an existing message. They first change it in the admin, and when they go to the site, the change is not visible. For example, if the old version was cached.

In fact, at the beginning I was sure that it was a browser caching error. But after some testing, things got a little weirder.

I found out that clearing the browser cache or using another browser does not solve the problem, but it is interesting that it switches between the old version and the modified version when updating.

So, if the message body was “Hello World” and I change it to “Goodbye cruel world” and then go to the site and refresh the page several times, I will see “Hello World”, then “Goodbye“ cruel world ”, then “Hello World,” etc. no matter how long I keep doing this.

But that does not stop there. after about 24 hours, everything returns to its place and works fine. No reshuffle anymore, the site is sticking to the new version ...

I almost got speechless because I built more than 50 other Django sites using the same server and I have never had this problem before.

I am using the latest django (1.3) with MySQL DB and caching is not enabled ..

Any ideas?

Edit : gracefully restarting Apache solves the problem .. but restarting apache after each update is not the biggest thing.

Update . I just configured my dev environement and I found out that the error is much more persistent with the dev server. A modified statement will not be displayed until I kill / restart the dev server, no matter how often I update or clear my cache.

+4
source share
3 answers

The problem is clearly addressed in the general views documentation . Requests in your extra_context dictionary are evaluated once when urlconf is first processed, and each time after that they will continue to use the same values. That's why they only change when you reset Apache or the dev server.

The solution, as described on the linked page, is to use calling calls that return queries, rather than asking queries in the dictionary itself.

+6
source

I had a similar problem. It turned out that I created an object at the top of urls.py and the object was alive while the process was alive. You can use a global variable in one of your views.

+3
source

There are several other ways to manage cache settings. For example, HTTP allows applications to do the following:

Determine the maximum time that the page should be cached. Specify whether the cache should always check for newer versions, only supplying cached content if there are no changes. ( Some caches may provide cached content even if the server page has changed, simply because the copy of the cache has not expired . **)

In Django, use the cache_control view decorator to specify these cache options. In this example, cache_control tells the caches to re-check the cache each time they access and store cached versions for a maximum of 3600 seconds:

 from django.views.decorators.cache import cache_control @cache_control(must_revalidate=True, max_age=3600) def my_view(request): # ... 

Any valid HTTP Cache-Control directive is valid in cache_control (). Here is the full list:

 public=True private=True no_cache=True no_transform=True must_revalidate=True proxy_revalidate=True max_age=num_seconds s_maxage=num_seconds 
+1
source

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


All Articles