Starts in:
3 DAYS
6 HRS
18 MIN
35 SEC
Starts in:
3 D
6 H
18 M
35 S

Microservice Solution: Distributed Cache

You must have an ABP Business or a higher license to be able to create a microservice solution.

Distributed caching is a common pattern in distributed systems to improve performance by storing frequently accessed data in a cache. This document explains how the distributed caching mechanism works in the microservice solution template. You can learn more in the Distributed Cache document.

Distributed Cache in Microservice Solutions

The microservice solution template uses Redis to implement the distributed caching mechanism. You can configure the Redis connection in the appsettings.json file of the related projects. Here is an example configuration:

  "Redis": {
    "Configuration": "localhost:6379"
  },
  "AbpDistributedCache": {
    "KeyPrefix": "Bookstore:"
  }

Additionally, you can configure the distributed cache key prefix in the ConfigureServices method. This approach helps prevent key conflicts if you use the same Redis server for multiple applications. Existing services in the microservice solution template already have this configuration. Here is an example configuration:

Configure<AbpDistributedCacheOptions>(options =>
{
    options.KeyPrefix = configuration["AbpDistributedCache:KeyPrefix"] ?? "";
});

The distributed caching mechanism is often needed in the application layer to cache frequently accessed data. You can inject the IDistributedCache<TCacheItem, TCacheKey> interface into your application service class and use it to cache data. Here is an example:

public class BookService : ITransientDependency
{
    private readonly IDistributedCache<BookCacheItem, Guid> _cache;

    public BookService(IDistributedCache<BookCacheItem, Guid> cache)
    {
        _cache = cache;
    }

    public async Task<BookCacheItem> GetAsync(Guid bookId)
    {
        return await _cache.GetOrAddAsync(
            bookId, //Guid type used as the cache key
            async () => await GetBookFromDatabaseAsync(bookId),
            () => new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)
            }
        );
    }
    private Task<BookCacheItem> GetBookFromDatabaseAsync(Guid bookId)
    {
        //TODO: get from database
    }
}

Lastly, you can use the Entity Cache feature, which offers a pre-built caching mechanism. If the requested item is not in the cache, it retrieves the data from the database, and when related data is manipulated, it automatically removes the item from the cache to ensure data consistency.


Contributors


Last updated: July 31, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More