Doubt cache Asp.net MVC

Asked

Viewed 196 times

2

I have the following scenario:

Public ActionResult ProdutoFornecedor01()
{
     var produtos = _db.Produtos.Include(x => x.Fornecedor).OrderByDescending(x => x.ProdutoId).Where(x => x.Fornecedor.Id == 1).Take(10);
     return PartialView("_PartialProdutosFornecedor01", produtos);
}

Public ActionResult ProdutoFornecedor02()
{
     var produtos = _db.Produtos.Include(x => x.Fornecedor).OrderByDescending(x => x.ProdutoId).Where(x => x.Fornecedor.Id == 2).Take(10);
     return PartialView("_PartialProdutosFornecedor02", produtos);
}

Public ActionResult ProdutoFornecedor03()
{
     var produtos = _db.Produtos.Include(x => x.Fornecedor).OrderByDescending(x => x.ProdutoId).Where(x => x.Fornecedor.Id == 3).Take(10);
     return PartialView("_PartialProdutosFornecedor03", produtos);
}

In my View Index I call the PartialView:

<div class="row">
     @Url.Action("ProdutoFornecedor01", "Produtos")
</div>

<div class="row">
     @Url.Action("ProdutoFornecedor02", "Produtos")
</div>

<div class="row">
     @Url.Action("ProdutoFornecedor03", "Produtos")
</div>

According to my Hosting Plan, there are many requests in the database.

I tried to use a Cache:

[OutputCache(Duration = 600, VaryByParam = "*")]

But the customer complained that when upgrading with a new product it takes to appear on Home.

What is the best way around this problem?

  • Friend, aside from the fact that you could have a generic vendor method instead of creating a method for each id, you could throw it all into memory so decreasing the amount of access to the bank, Try to allocate this data in memory and only make a request in the database if this memory is empty. If any registration occurs save and update this memory.

2 answers

1

You can keep the cache, and during the insertion of a product by the customer, update the cache referring to the list of products of this client, as if it were a TRIGGER.

This way the cache is only updated when a product is inserted, I suppose it is less frequently and will decrease the access to BD.

1

@Hermes

First, it will depend on the amount of records you are sending. You can async call with the Entity Framework

One of the ways to load more "fast" your home, is you load your views async with ajax.

In the following way: In your view

<div class="row">
   <div class="partial" [email protected]("Action","Controller")></div>
</div>
<div class="row">
   <div class="partial" [email protected]("Action","Controller")></div>
</div>
<div class="row">
   <div class="partial" [email protected]("Action","Controller")></div>
</div>

and his js:

$('.partial').each(function(index,item) { 
   var url = $(item).data("url");
   if(url && url.length >0) {
     $(item).load(url);
   }
});
  • The quantity will always be the same. Always list for example the last 10 products registered by category. I found very interesting your example, but would request the database the same way, I need something to decrease the requests.

  • @Hermesautran Seeing by your code, you search all the information of Products and Suppliers, already tried to use select and search only what you need?

  • I edited the question. In this case, I will search the 10 last product records of each Supplier that I wish to show at Home. As the products are included by each Vendor at random, it would not be cool to give a select and bring all the products and then separate them right?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.