6

Improvements to the Caching Abstraction in ASP.NET Core

 11 months ago
source link: https://devblogs.microsoft.com/dotnet/caching-abstraction-improvements-in-aspnetcore/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Improvements to the Caching Abstraction in ASP.NET Core

a43dbc08ce77efb88c93ced340edd1f5?s=58&d=mm&r=g

Catherine Wang

marcgravell-96x96.png

Marc Gravell

July 6th, 20230 0

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.

Marc Gravell Principal Software Engineer, Distributed Systems

Follow


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK