Monday, January 6, 2020

Cache Performance


Performance is one of the key success metric for any public website to be adopted. Ideally, any website should server the end customer in sub second (less than a second); at a max of 3 seconds.

If a website takes more than 3 seconds to respond, the user doesn't usually go back. Google and other search engines also prefer to recommend optimized, mobile-friendly, and faster websites.

One common way to improve the website performance, is using 'Cache' concept. We can increase the performance of the application if we can reduce the number of requests to the server every time.

On the first request, client web app requests the server and get the response that is persisted at client side for the future reference. Local storage is kept for pre defined time frame.  Within that time frame, the subsequent client side calls are served locally, in stead of hitting remote server. This Caching concept reduces the signficant time & effort in Web Architecture.

Let us implement it now using ASP .NET Core technology.

    public async Task GetCacheData() 
    { 
        var cacheEntry = await 
            _cache.GetOrCreateAsync(CacheKeys.Entry, entry => 
        { 
            entry.SlidingExpiration = TimeSpan.FromSeconds(120); 
            return Task.FromResult(DateTime.Now); 
        }); 
     
        return View("Cache", cacheEntry); 
    } 



Caching the content helps us to reduce server calls again and again and it helps us to increase the performance of the application.  ASP .NET Core supports multiple caching techique like In-Memory, Response and Distributed Caching.

More details at https://docs.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-2.2

No comments:

Post a Comment