Good day. I am trying to optimize the work of Autocomplete search string on the site. The problem is that when you enter search text into the controller, requests are received by letter. For example, when searching for the word "hello", requests for "p", "pr", "at", "pre", "pre", "hello" come in parallel. When receiving the next request within the session, it is necessary to stop the controller of previous requests. For this, I think we need a global variable that takes values ​​during these queries. I tried Session and TempData - it did not work, because It seems that their values ​​are transmitted to the global variable after the controller ends or when the response from the browser is received. I hope clearly explained.

  • Cancel requests on the client side. if the user prints quickly, it will effectively interrupt the request before it reaches the server. - PashaPash
  • and do not optimize it on the server. such requests should be easy, you will spend more server resources on synchronization, interruption and session support, than on just processing all requests - PashaPash

1 answer 1

You overcomplicate. In the case of auto-completion, it’s enough just to adjust its sensitivity. For example, send a request to the server only if the input field contains at least 3 characters. For example, if you use JqueryUI, you can do it like this:

$( ".selector" ).autocomplete({ minLength: 3 }); 

Using any global variables and keeping track of previous queries is completely wrong. Firstly, global variables themselves are a bad idea, and secondly, imagine the situation: two different users enter some lines into your autocompet about the same time. Say, the first one entered "cat", his request went to the server. Then the second wants to enter the "cottage", and his request also went to the server. According to your logic, it turns out that it is necessary to cancel the first request, and, accordingly, the first user will either receive an answer that nothing has been found about the cat, or he will have to wait for the second user to complete the request, and get information not about the cat, but about the cottage. Is this the correct behavior? I doubt it.

UPD

If the query to the database is very slow, then, firstly, try to optimize it or add the necessary indexes, and secondly, add caching of the results of this query in order not to refer to the database each time, but to take ready data from the cache. As a rule, the data returned by the autocomplete is quite static data (for example, city-countries) and does not change often, so the cache lifetime can be quite long. The idea with a delay proposed by @ tym32167, by the way, is also quite suitable. Let unnecessary clicks drop out on the client, not on the server.

  • one
    I would add a delay of 0.5-1 seconds before sending the request. That the request left only if the user ceased to print. - tym32167
  • I have minLength. The idea is that the variable should be within the session, not the global site. Just sampling from the database is hard and minLength helps partially. Well, a delay of 0.5-1 seconds is still a delay. - Frol4ic
  • @ Frol4ic updated the answer - DreamChild
  • one
    @ Frol4ic In general, such a delay is a common practice - in order not to overload the server. Why should the user send requests to the server if he has not finished entering? - tym32167
  • @ Frol4ic In fact, you also have the problem that you are sending unnecessary requests, and then you want to cancel them. I just suggested not to send them :) - tym32167