From 4c681af15460121cdf8b5eb3b9ae41f47f94afd3 Mon Sep 17 00:00:00 2001 From: czd890 Date: Mon, 4 Dec 2023 16:08:47 +0800 Subject: [PATCH] Add an option to support to disable local file cache --- src/Apollo.Configuration/ApolloOptions.cs | 2 ++ src/Apollo.ConfigurationManager/Util/ConfigUtil.cs | 3 +++ src/Apollo/IApolloOptions.cs | 3 +++ src/Apollo/Internals/ConfigRepositoryFactory.cs | 8 ++++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Apollo.Configuration/ApolloOptions.cs b/src/Apollo.Configuration/ApolloOptions.cs index 1f3d717..f4bc23e 100644 --- a/src/Apollo.Configuration/ApolloOptions.cs +++ b/src/Apollo.Configuration/ApolloOptions.cs @@ -96,6 +96,8 @@ public virtual string? MetaServer /// ms. Default 300,000ms public virtual int RefreshInterval { get; set; } = 5 * 60 * 1000; //5 minutes + public bool EnableLocalFileCache { get; set; } = true; + public string? LocalCacheDir { get; set; } public IDictionary Meta { get; set; } = new Dictionary(StringComparer.OrdinalIgnoreCase); diff --git a/src/Apollo.ConfigurationManager/Util/ConfigUtil.cs b/src/Apollo.ConfigurationManager/Util/ConfigUtil.cs index 25e14cc..5764379 100644 --- a/src/Apollo.ConfigurationManager/Util/ConfigUtil.cs +++ b/src/Apollo.ConfigurationManager/Util/ConfigUtil.cs @@ -2,6 +2,7 @@ using Com.Ctrip.Framework.Apollo.Enums; using Com.Ctrip.Framework.Apollo.Foundation; using Com.Ctrip.Framework.Apollo.Logging; + using System.Collections.ObjectModel; using System.Configuration; @@ -187,6 +188,8 @@ private void InitRefreshInterval() public int RefreshInterval => _refreshInterval; + public bool EnableLocalFileCache => bool.TryParse(GetAppConfig(nameof(EnableLocalFileCache)), out var enableLocalFileCache) && enableLocalFileCache; + public string LocalCacheDir => GetAppConfig(nameof(LocalCacheDir)) ?? Path.Combine(ConfigConsts.DefaultLocalCacheDir, AppId); public bool EnablePlaceholder => bool.TryParse(GetAppConfig(nameof(EnablePlaceholder)), out var enablePlaceholder) && enablePlaceholder; diff --git a/src/Apollo/IApolloOptions.cs b/src/Apollo/IApolloOptions.cs index 5c2e36b..6a38bb8 100644 --- a/src/Apollo/IApolloOptions.cs +++ b/src/Apollo/IApolloOptions.cs @@ -1,4 +1,5 @@ using Com.Ctrip.Framework.Apollo.Enums; + using System.Collections.ObjectModel; namespace Com.Ctrip.Framework.Apollo; @@ -44,6 +45,7 @@ public interface IApolloOptions : IDisposable /// Refresh interval. ms int RefreshInterval { get; } + bool EnableLocalFileCache { get; } string? LocalCacheDir { get; } HttpMessageHandler HttpMessageHandler { get; } @@ -57,4 +59,5 @@ public interface IApolloOptions : IDisposable #else IReadOnlyCollection? SpecialDelimiter { get; } #endif + } diff --git a/src/Apollo/Internals/ConfigRepositoryFactory.cs b/src/Apollo/Internals/ConfigRepositoryFactory.cs index b506d36..c81fa64 100644 --- a/src/Apollo/Internals/ConfigRepositoryFactory.cs +++ b/src/Apollo/Internals/ConfigRepositoryFactory.cs @@ -30,8 +30,12 @@ private IConfigRepository CreateConfigRepository(string @namespace) LogManager.CreateLogger(typeof(ConfigRepositoryFactory)).Warn($"==== Apollo is in local mode! Won't pull configs '{@namespace}' from remote server! ===="); return new LocalFileConfigRepository(@namespace, _options); } - - return new LocalFileConfigRepository(@namespace, _options, new RemoteConfigRepository(@namespace, _options, _httpUtil, _serviceLocator, _remoteConfigLongPollService)); + var remoteRepo = new RemoteConfigRepository(@namespace, _options, _httpUtil, _serviceLocator, _remoteConfigLongPollService); + if (!_options.EnableLocalFileCache) + { + return remoteRepo; + } + return new LocalFileConfigRepository(@namespace, _options, remoteRepo); } public void Dispose()