Skip to content

Commit

Permalink
Add a flag to control whether save the config to local as cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Mar 21, 2024
1 parent 50c05bb commit 5449eda
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions AgileConfig.Client/ConfigClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ await _WebsocketClient.SendAsync(new ArraySegment<byte>(data, 0, data.Length), W

private void WriteConfigsToLocal(string configContent)
{
if (!_options.CacheEnabled)
{
return;
}

try
{
if (string.IsNullOrEmpty(configContent))
Expand Down
21 changes: 16 additions & 5 deletions AgileConfig.Client/ConfigClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ConfigClientOptions

public int HttpTimeout { get; set; } = 100;

public bool CacheEnabled { get; set; } = true;

public string CacheDirectory { get; set; }
/// <summary>
/// 缓存加密
Expand Down Expand Up @@ -135,12 +137,14 @@ public static ConfigClientOptions FromConfiguration(IConfiguration config)
var timeout = config["AgileConfig:httpTimeout"];
var cacheDir = config["AgileConfig:cache:directory"] ?? "";
var cacheEncrypt = config.GetValue("AgileConfig:cache:config_encrypt", false);
var cacheEnabled = config.GetValue("AgileConfig:cache:enabled", true);
options.Name = name;
options.Tag = tag;
options.AppId = appId;
options.Secret = secret;
options.Nodes = serverNodes;
options.ENV = string.IsNullOrEmpty(env) ? "" : env.ToUpper();
options.CacheEnabled = cacheEnabled;
options.CacheDirectory = cacheDir;
options.ConfigCacheEncrypt = cacheEncrypt;
if (int.TryParse(timeout, out int iTimeout))
Expand Down Expand Up @@ -234,14 +238,21 @@ private static string TryGetIdFromLocal(string cacheDir, string appId)

private static void WriteIdToLocal(string cacheDir, string appId, string serviceId)
{
if (!string.IsNullOrWhiteSpace(cacheDir) && !Directory.Exists(cacheDir))
try
{
Directory.CreateDirectory(cacheDir);
}
if (!string.IsNullOrWhiteSpace(cacheDir) && !Directory.Exists(cacheDir))
{
Directory.CreateDirectory(cacheDir);
}

string idFileName = Path.Combine(cacheDir, $"{appId}.agileconfig.client.serviceid");
string idFileName = Path.Combine(cacheDir, $"{appId}.agileconfig.client.serviceid");

File.WriteAllText(idFileName, serviceId);
File.WriteAllText(idFileName, serviceId);
}
catch (Exception e)
{
DefaultConsoleLogger.LogError(e, "Can not save serviceId to local");
}
}

private static ILogger _consoleLogger;
Expand Down
4 changes: 4 additions & 0 deletions AgileConfig.Client/RegisterCenter/DiscoveryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ private void EnsureCacheDir()

private void WriteServiceInfosToLocal(string content)
{
if (!_options.CacheEnabled)
{
return;
}
try
{
if (string.IsNullOrEmpty(content))
Expand Down

0 comments on commit 5449eda

Please sign in to comment.