F
F
forven2015-02-14 18:04:00
ASP.NET
forven, 2015-02-14 18:04:00

How to create variables and store them until a certain moment?

I perform a database search using the parameters specified by the user. The result is 2 arrays with search results. How to store them and issue them in parts on a page-by-page basis ("pagination"?)?
The use of skip does not suit me, because the search will have to be constantly used, I think that in my crooked algorithm this will cause problems.
I allow the option of creating a request object, where the request parameters and 2 arrays with results will be stored, and for example, with a new request, it will be replaced with a new one, and deleted when the search page is closed. But I don’t know how to implement this, and what are the disadvantages.
What other options are there to search the database once and store the results until they are no longer needed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Nemiro, 2015-02-14
@forven

Typically, the search is performed once. To improve performance, search results are cached and retrieved from the cache on subsequent accesses. The cache can be stored anywhere. The data is automatically deleted after the specified lifetime.
How to issue data in parts depends on how they are obtained.
Pagination can be implemented from the database side and get data only for a particular page. I will not show the code, there may be many options. The decision will depend on the amount of data, the resource intensity of search queries.
As for caching, the simplest option is:

// или HttpContext.Current.Cache
if (HttpContext.Cache["Ключ"] == null) 
{
  // данных в кэше нет, получаем из базы и добавляем в кэш
  HttpContext.Cache.Add
  (
    "Ключ",
    "тут сами данные",
    null,
    DateTime.Now.AddMinutes(5), // срок годности пять минут
    TimeSpan.Zero,
    System.Web.Caching.CacheItemPriority.Normal,
    null
  );
}
else
{
  // есть данные в кэше, используем их
  var data = HttpContext.Cache["Ключ"]; // типы строгие лучше использовать
}

Or use the OutputCache attribute and cache pages.
Additionally, you can create sessions in the database with links to search results. Those. perform a search, write the key to the table, then make a selection using the found keys. Outdated results can be removed, or by ASP.NET , periodically run the query. Or, if possible, configure SQL Server Agent . This is if there is a lot of data and the search is expensive.

C
CrazyHorse, 2015-02-14
@CrazyHorse

"What other options are there to search the database once and store the results until they are no longer needed?" And what will you do if the database changes between two queries? show the user stale data? or for example you have a huge database - are you going to pull it all out and then filter by this data set?
I would look towards the MvcPaging package

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question