Centralized Configuration Management using Azure App Configuration: Setting Up Offline Caching

Centralized Configuration Management using Azure App Configuration: Setting Up Offline Caching

Centralized Configuration Management using Azure App Configuration Series

In the previous article we talked about how we can configure Dynamic Configuration Refresh for Azure App Configuration. In this article we’ll look at how you can configure offline caching for Azure App Configuration.

Setting up Offline Caching for App Configuration is pretty easy. You need to have an implementation of IOfflineCache interface and out of the box there is an OfflineFileCache implementation provided to you. All you need to do is configure this offline cache implementation in the Program.cs file.

using Azure.Identity;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;

namespace MusicStore.Web
{
  public class Program
  {
    ..

    public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
          webBuilder.UseStartup<Startup>();
        })
        .ConfigureAppConfiguration((context, config) => {
          var settings = config.Build();
          var appConfigEndpoint = settings["AppSettings:AppConfiguration:Endpoint"];
          var userAssignedIdentityClientId = settings["AppSettings:Identity:ClientId"];

          if (!string.IsNullOrEmpty(appConfigEndpoint))
          {
            var endpoint = new Uri(appConfigEndpoint);

            config.AddAzureAppConfiguration(options =>
            {
              options
                // Use managed identity to access app configuration
                .Connect(endpoint, new ManagedIdentityCredential(clientId: userAssignedIdentityClientId))

                // Setup dynamic refresh
                .ConfigureRefresh(refreshOpt =>
                {
                    refreshOpt.Register(key: "AppSettings:Version", refreshAll: true, label: LabelFilter.Null);
                    refreshOpt.SetCacheExpiration(TimeSpan.FromSeconds(10));
                })

                // Setup offline cache
                .SetOfflineCache(new OfflineFileCache())
                .UseFeatureFlags();
            });
          }
        });
  }
}

All you need to do is to use the SetOfflineCache() method in the AzureAppConfigurationOptions and supply the method with an implementation of IOfflineCache interface. The OfflineFileCache implementation uses the local file system to cache the app configuration and supports the encryption of the stored data.

When you configure Offline caching, every time App Configuration tries to fetch the configuration from Azure, it will check the cache and if it’s expired fetch the values from Azure and update the local cache.

Summary

In this short article we looked at how we can configure offline caching for App Configuration using the built in OfflineFileCache implementation that uses the local file system. The source code for this example is available in the Azure App Configuration Offline Cache Example GitHub repository. In the next article we’ll look at how we can create our own caching implementation for offline caching.

You Might Also Like
Comments