Appconfiguration: Offline Cache

Created on 11 Mar 2019  路  6Comments  路  Source: Azure/AppConfiguration

We are building many microservices that might depend on Service Registry based Key Value Store. We originally thought of using either Consul from HashiCorp or use VSTS as the store since it has release variables.
However We are now thinking of using AppConfiguration for that purpose.,
Since this is going to be central configuration store, we do not want our services go kaboom when the AppConfiguration goes down for any odd reason.
I do see SetOfflineCache method. I am not sure if it is for this purpose of caching the values as a backup?

Are there any examples for SetOfflineCache?

OfflineCache

Most helpful comment

Hi @kamalsivalingam, yes, the idea behind the offline cache is to save a last known good (LKG) copy of config that the application requested previously to the local environment of the user's application, so the application can still use LKG config when unexpected happens (for example, a network hiccup at the application start-up).

However, the users' application environments can be very different from one to another, so the solution of caching differs too. Each environment will need its implementation of IOfflineCache. Currently, the config provider provides offline file cache for applications hosted in Azure App Service. The caching file is encrypted and saved at $home/data/azureAppConfigCache (so the cache can be shared among multiple web workers). Here is a code snippet to enable it.

config.AddAzureAppConfiguration(option =>
{
    option.Connect(settings["ConnectionStrings:AppConfig"])
          .SetOfflineCache(new OfflineFileCache());
});

Please let us know if this is something you are looking for. Any feedback is welcome.

All 6 comments

Hi @kamalsivalingam, yes, the idea behind the offline cache is to save a last known good (LKG) copy of config that the application requested previously to the local environment of the user's application, so the application can still use LKG config when unexpected happens (for example, a network hiccup at the application start-up).

However, the users' application environments can be very different from one to another, so the solution of caching differs too. Each environment will need its implementation of IOfflineCache. Currently, the config provider provides offline file cache for applications hosted in Azure App Service. The caching file is encrypted and saved at $home/data/azureAppConfigCache (so the cache can be shared among multiple web workers). Here is a code snippet to enable it.

config.AddAzureAppConfiguration(option =>
{
    option.Connect(settings["ConnectionStrings:AppConfig"])
          .SetOfflineCache(new OfflineFileCache());
});

Please let us know if this is something you are looking for. Any feedback is welcome.

This is Awesome :..All the problems we wanted to solve using custom implementation is already taken care
:100:

does this also work locally? is %HOME% used there?

@vip32 when your app is not hosted in App Service, you can set your own offline file path via OfflineFileCacheOptions.Path. For example,

config.AddAzureAppConfiguration(option =>
{
    option.Connect(settings["ConnectionStrings:AppConfig"])
          .SetOfflineCache(new OfflineFileCache(
            new OfflineFileCacheOptions() { Path = @"D:\AppConfig\OfflineCache.json" }));
});

Hello zhenlan,
I am getting the following error when using the above way.

System.Private.CoreLib: Exception while executing function: Function1. AzureKeyVault: The type initializer for 'AzureKeyVault.Function1' threw an exception. System.Text.Json: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.

P.S. I have created File "OfflineCache.json" before setOfflineCache() as it was throwing error for not finding the file

@vip32 Does this work for IIS hosted apps as well? We still have apps hosted in IIS that have yet to move to the cloud we want them to consume remote configs as well.

Was this page helpful?
0 / 5 - 0 ratings