A
A
abbrakadabbra2019-07-01 11:16:42
Django
abbrakadabbra, 2019-07-01 11:16:42

How to optimize/cache slow code parts in Django?

The project is written in Django + Postgres. A certain access system has been built based on a variety of criteria, where it is not possible to find out whether a user has access somewhere with a simple request.
A chain of methods is executed, inside of which there are also cycles that make up Q (). Unfortunately, it is not easy to rewrite it now, because. the entire project is built on this access system.
Perhaps this will somehow be corrected, but at the moment, caching can probably be the solution.
Possible Solution
: to receive an "access card" for some userstakes up to 30 seconds. We thought that her access card could be stored in the cache (redis) at the user authorization stage by sending a celery task in parallel. The result of the task execution will be stored in the cache, which can be accessed later, with a faster execution time.
Because the access card can change, with some changes, you can send a signal to "re-execute" it.
What do you think, is such an idea working and what pitfalls can there be?
PS Requests were individually debugged via the debug toolbar, and they are fast enough. The problem is loops and overall execution/render time. Tables have indexes. There are no complaints about the DB.
PSI repeat once again that the main methods that check something in the cycle. do Q() etc. cannot be rewritten now.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-07-01
@sergey-gornostaev

I recently ran into a similar situation and just moved the curly logic into stored procedures.

V
Vadim Shatalov, 2019-07-01
@netpastor

It also helps to replace the chain of requests with one view in the database and access it through the unmanaged model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question