Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using c# linq to populate/load a list one time to enhance performance

I'm working on an ASP.NET MVC 4.5 application. I need to populate a list in a dropdown. I t works fine, anyhow, when I click the field there is allways a server request.

I'd like to store the values after the initial load on the client side to speed up the application.

I'm using a ViewModel to populate the list in my Razor View.

Do you know how to achieve an initial load of the 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);
}

The Razor View:

<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>

Many thanks and kind regards!

like image 630
TimHorton Avatar asked Mar 30 '17 12:03

TimHorton


People also ask

What is C in used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

How do I use C on my computer?

This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute). The C program is the human-readable form, while the executable that comes out of the compiler is the machine-readable and executable form.

Why do people use C?

C is mostly used in creating popular compilers . Many programming language compilers are developed with reference from C. This is due to the association of C to low-level programming languages. This eases the language to be comprehensible to machines.

Is C still useful?

The C programming language has been alive and kicking since 1972, and it still reigns as one of the fundamental building blocks of our software-studded world.


2 Answers

You can try to use 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 - Specifies that you want to cache the results on client only. There are other options available.

VaryByParam = "id;query" - Is needed to differ cache results based on method arguments.

Duration - cache duration in seconds.

like image 168
Alex Art. Avatar answered Oct 17 '22 07:10

Alex Art.


You need some caching stratergies here is 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;
}

Usage:

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

Cache provider will check if there's anything by the name of "CACHEKEY" in the cache, and if there's not, it will call a delegate method to fetch data and store it in cache.

Example:

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

In your case it would 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 customize according to your needs as well

Using these caching strategies it will first time load data and store in cache in second time it will get value from cache instead of round trip to database.

like image 30
Curiousdev Avatar answered Oct 17 '22 09:10

Curiousdev