Skip to main content

Improvements to the Caching Abstraction in ASP.NET Core

We are improving the caching abstraction in .NET to make it more intuitive and reliable. This blog showcases a sample project with reusable extension methods for Distributed Caching to greatly simplify the repetitive code on object serialization when setting cached values. It also provides guidance on best practices so developers can focus on business logic. We are actively working on bringing this experience into the .NET and our current ETA is post .NET 8, but we wanted to share early progress and get feedback.

Background

Application developers mostly use distributed caches to improve data query performance and to save web application session objects. While the concept of caching sounds straight forward, we heard from .NET developers that it requires a decent amount of experience to use caching properly. The challenges are:

  • The required object serialization code is repetitive and error prone.
  • It’s still a common error to use sync over async in caching methods with clients such as StackExchange.Redis
  • There is too much overhead from “boilerplate” type of work in .NET caching methods.

Ideally, the developer should only have to specify what to cache, and the framework should take care of the rest. We are introducing a better caching abstraction in .NET to optimize for common distributed caching and session store caching scenarios. We would like to show you a concept of our solution, hoping to solve some immediate problems and receive feedback. The sample extension methods are written by Marc Gravell, the maintainer of the popular StackExchange.Redis. You can copy the DistributedCacheExtensions.cs into your ASP.NET core web project, configure a distributed cache in Program.cs, and follow example code to start using the extension methods.

The extension methods sample code is very intuitive if you want to jump directly into it. The rest of the blog dives into the detailed implementation behind the extension methods with an example Web API application for a weather forecast.

Application scenarios for using the new Distributed Cache extension methods

In the extension methods sample code, only GetAsync() methods are exposed. You may wonder where the methods for setting cache values are. The answer is – you don’t need the Set methods anymore. Set methods are abstracted by the GetAsync() implementations when reading value from the data source upon a cache miss. The GetAsync() methods essentially do the following:

  • Automate the Cache-Aside pattern. This means it always attempts to read from cache. In the case of a cache miss, the method executes a user-specified function to return the value and save it in cache for future reads.
  • Object serialization. The extension methods allow developers to specify what to cache. No custom serialization code needed. The sample code uses Json serializer, but you can edit the code to use protobuf-net or other custom serializers for performance optimization.
  • Thread management. All caching methods are designed to be asynchronous and work reliably with synchronous operations that generate the cache values.
  • State management. The user-specified functions can optionally be stateful with simplified coding syntax, using static lambdas to avoid captured variables and per-usage delegate creation overheads.

Example for using the sample code

The WeatherAPI-DistributedCache demo shows how to easily re-use the extension methods, with Azure Cache for Redis as example.

  1. Copy the DistributedCacheDemo/DistributedCacheExtensions.cs to your ASP.NET Core project in the same folder directory as the Program.cs file. Per Figure 1, the DistributedCacheExtensions.cs code file is placed alongside Program.cs in the project folder.

    Figure 1: Copy and paste the DistributedCacheExtensions.cs file to your web project

    Placing the sample DistributedCacheExtensions.cs code in the same folder as program.cs

  2. Add the Distributed Cache service in Program.cs by:
    1. Adding Microsoft.Extensions.Caching.StackExchangeRedis to your project
    2. Adding the following code:
      builder.Services.AddStackExchangeRedisCache(options =>
      
      {
      
      options.Configuration = builder.Configuration.GetConnectionString("MyRedisConStr");
      
      options.InstanceName = "SampleInstance";
      
      });
  3. Use the extension methods in Weather Controller class that contains business logic. Per Figure 2, include the using statement to access the extension methods.

    Figure 2: add using statement in the class to access the extension methods

    Example of including the sample extension code through using statement

  4. Refer to the sample code to use the new GetAsync() methods. The only code needed is the business logic for generating weather forecast data. All caching operations are abstracted by the extension methods. Figure 3 shows a user defined method for generating the weather forecast for the next week, which is the only business logic required from developers as the input to use caching methods.

    Figure 3: define the business logic to use the new GetAsync() extension methods

    Example of using GetAsync() method by only adding the business logic for generating the cached value if not found

Notice that no object serialization code is required to get or set an WeatherForecast object in the cache. And in fact, only caching options and cache key name are needed as parameters to use the GetAsync() method in a web application. Developers can entirely focus on the business logic without any “boilerplate” efforts to get caching operations to work.

Next steps

Try out the concept from our sample code today! You can provide feedback by leaving comments in this blog post or filing an issue in the ASP.NET Core repository. If you are looking for guidance on using a distributed cache to improve you cloud application’s performance, we have an example at Improving Web Application Performance Using Azure Cache for Redis.

The post Improvements to the Caching Abstraction in ASP.NET Core appeared first on .NET Blog.



Comments

Popular posts from this blog

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee