Using C # linq to populate / load a list once to improve performance

I am working on an ASP.NET MVC 4.5 application. I need to populate a drop down list. I don’t work fine, anyway, when I click on the field, there is always a server request.

I want to save the values ​​after bootstrapping on the client side in order to speed up the application.

I am using ViewModel to populate a list in a Razor view.

Do you know how to achieve bootstrap of this?

Here is my DataSource controller:

public JsonResult GetBusinessLineList(string id = null, string query = null) { return Json(Db.BusinessLine.AsQueryable() .Where(x => x.Label.Contains(query)) .Take(10) .Select(x => new { id = x.BusinessLineId, text = x.Label }) .ToList(), JsonRequestBehavior.AllowGet); } 

Shaver Type:

 <div class="form-group"> @Html.LabelFor(model => model.BusinessLineIdList, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.Select2AjaxFor(model => model.BusinessLineIdList, @Url.Action("GetBusinessLineList", "DataSource"), new { @class = "form-control")) </div> </div> 

Thanks a lot and are kind!

+5
source share
2 answers

You can try using the output cache:

 [OutputCache(Location = OutputCacheLocation.Client, VaryByParam = "id;query", Duration = 3600)] public JsonResult GetBusinessLineList(string id = null, string query = null) { return Json(Db.BusinessLine.AsQueryable() .Where(x => x.Label.Contains(query)) .Take(10) .Select(x => new { id = x.BusinessLineId, text = x.Label }) .ToList(), JsonRequestBehavior.AllowGet); } 

OutputCacheLocation.Client - Indicates that you want to cache the results only on the client. There are other options.

VaryByParam = "id;query" - it is necessary to distinguish cache results based on the arguments of the method.

Duration - cache duration in seconds.

+7
source

You need some caching stratergies, this is a simple cache helper class

 using System.Runtime.Caching; public class cacheservice: ICacheservice { public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class { T item = MemoryCache.Default.Get(cacheKey) as T; if (item == null) { item = getItemCallback(); MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(10)); } return item; } } interface ICacheService { T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class; } 

Using:

 cacheservice.GetOrSet("CACHEKEY", (delegate method if cache is empty)); 

The cache provider checks to see if there is anything named "CACHEKEY" in the cache, and if not, it will call the delegate method to retrieve the data and store it in the cache.

Example:

 var Data=cacheService.GetOrSet("CACHEKEY", ()=>SomeRepository.GetData()); 

In your case, it will be like

 var Data=cacheService.GetOrSet("CACHEKEY", Db.BusinessLine.AsQueryable() .Where(x => x.Label.Contains(query)) .Take(10) .Select(x => new { id = x.BusinessLineId, text = x.Label }) .ToList()); 

You can also customize according to your needs.

Using these caching strategies, it will first load the data and store it in the cache a second time, and will receive the value from the cache instead of going back to the database.

+2
source

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


All Articles