Microservice Solution: Distributed Cache

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.

Was this page helpful?
Please make a selection.
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

testtestOtherLiveEvents

19 Jun, 03: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