Using ITwoLayerCache

If cached data that changes infrequently that the best solution can be ITwoLayerCache, which assumes keeping data in a database but duplicates it in the memory of the web server. Thus you reduce interactions with a database and consequently improve performance. There is one implementation of this abstraction - TwoLayerCache. The TwoLayerCache depends on IInMemoryCache and ISharedCache implicitly. The former is the first layer of the caching, the latter is the second. To avoid cache consistency problems you also need to provide ITwoLayerCacheStateObserver implementation.

Next instruction shows how to use ITwoLayerCache based on Redis as the second layer of caching and RabbitMQ for the cache synchronization.

1. Install Redis

2. Install RabbitMQ

3. Install next packages:

dotnet add package InfinniPlatform.Cache.Memory -s https://www.myget.org/F/infinniplatform/
dotnet add package InfinniPlatform.Cache.Redis -s https://www.myget.org/F/infinniplatform/
dotnet add package InfinniPlatform.Cache.TwoLayer -s https://www.myget.org/F/infinniplatform/
dotnet add package InfinniPlatform.MessageQueue.RabbitMQ -s https://www.myget.org/F/infinniplatform/

4. Update ConfigureServices() as below:

 using System;

 using InfinniPlatform.AspNetCore;

 using Microsoft.Extensions.DependencyInjection;

 public class Startup
 {
     public IServiceProvider ConfigureServices(IServiceCollection services)
     {
         services.AddInMemoryCache();
         services.AddRedisSharedCache();
         services.AddTwoLayerCache();
         services.AddRabbitMqMessageQueue();

         // ...

         return services.BuildProvider();
     }

     // ...
 }

5. Request the ITwoLayerCache instance in the constructor:

 class MyComponent
 {
     private readonly ITwoLayerCache _cache;

     public MyComponent(ITwoLayerCache cache)
     {
         _cache = cache;
     }

     // ...
 }