0

I am working in a multi-layered web application that has ASP.NET MVC as its front-end client. A particular page of this web application is taking a very long time to load. Around 30 seconds.

I downloaded dotTrace and ran it on my application (following this tutorial). I found out that the reason my application is slow.

It turns out it is because one particular method that I have does a load of work (takes time), and that same method gets called a total of 4 times.

Here is a screenshot from dotTrace showing the above:

dotTrace screenshot

The method in question is GetTasks(). So in order to improve the speed of the web application I want to cache the data returned from GetTasks() for each request.

If my thinking is correct, this would really improve on the speed issues I am having.

My question is, how can I achieve this? I have never done such a thing before. For each new request, how can I cache the data returned from GetTasks(), and use that for all subsequent calls to GetTasks().

halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • Do your tasks have some sort of unique identifier you can recognize them by? – Yuval Itzchakov Aug 04 '14 at 12:38
  • Yeah, they have an Id? – J86 Aug 04 '14 at 12:39
  • 1
    Caching *can* often help considerably. One thing to also check is what's actually going across the wire. Are the queries efficient? I found that many of my queries where all that efficient initially and needed some tweaking (indexes) to improve things. – itsmatt Aug 04 '14 at 12:43
  • @itsmatt thanks yep, that is what I'll do next. First need to get the caching working. – J86 Aug 04 '14 at 12:44

2 Answers2

1

Have you considered the Cache Aside pattern?

You can implement it easily using LazyCache

//probably in my constructor (or use dependency injection)
this.cache = new CachingService()

public List<MyTasks> GetTasks() 
{
    return cache.GetOrAdd<List<MyTasks>>("get-tasks", () = > {
        //go and get the tasks here.
    });
}

For more information see https://alastaircrabtree.com/the-easy-way-to-add-caching-to-net-application-and-make-it-faster-is-called-lazycache/

alastairtree
  • 3,960
  • 32
  • 49
0

One of the most popsular solution is to cache results. I can shouw you my solution. First of all install Nuget package: LazyCache Then you can use wrapper that I've created wrapper: code. You can extract and interface or whatever.

Then you can use it like this:

private readonly CacheManager cacheManager = new CacheManager(); 
          // or injected via ctor

public IEnumerable<Task> GetTasks()
{
    return this.cacheManager.Get("Tasks", ctx => this.taskRepository.GetAll());
}

public void AddTask(Task task)
{
    this.taskRepository.Create(task);
    /// other code

    // we need to tell the cache that it should get fresh collectiion
    this.cacheManager.Signal("Tasks"); 
}
  • I did [something similar](http://pastebin.com/b4v5TiYW) following an article from Microsoft, but not getting any performance improvement. I'm guessing I'm messing it up somewhere! – J86 Aug 04 '14 at 13:49
  • Without the code for CacheManager this answer seems a little incomplete. – alastairtree Feb 27 '18 at 11:46