diff --git a/XCode/Cache/DataCache.cs b/XCode/Cache/DataCache.cs index 0e2221b4f..0eaf8f5af 100644 --- a/XCode/Cache/DataCache.cs +++ b/XCode/Cache/DataCache.cs @@ -1,6 +1,8 @@ using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using System.Text; using NewLife; +using NewLife.Common; using NewLife.Serialization; using NewLife.Threading; @@ -10,10 +12,9 @@ namespace XCode.Cache; public class DataCache { #region 静态 - private static readonly String _File = @"Config\DataCache.config"; private static DataCache? _Current; /// 当前实例 - public static DataCache Current => _Current ??= Load(_File.GetBasePath(), true)!; + public static DataCache Current => _Current ??= Load(); #endregion #region 属性 @@ -22,6 +23,36 @@ public class DataCache #endregion #region 方法 + private static DataCache Load() + { + var path = Path.GetTempPath().CombinePath(SysConfig.Current.Name); + var file = path.CombinePath("DataCache.config").GetBasePath(); + var old = @"Config\DataCache.config".GetBasePath(); + + if (!File.Exists(file) && File.Exists(old)) + { + try + { + File.Move(old, file); + } + catch + { + file = old; + } + } + + return Load(file, true)!; + } + + private static void Save(DataCache? data) + { + var path = Path.GetTempPath().CombinePath(SysConfig.Current.Name); + var file = path.CombinePath("DataCache.config").GetBasePath(); + + if (data != null) + Save(file, data); + } + /// 加载 /// /// @@ -67,7 +98,7 @@ public static void Save(String file, DataCache data) /// 异步保存 public void SaveAsync() { - _timer ??= new TimerX(s => Save(_File.GetBasePath(), (s as DataCache)!), this, 100, 10 * 60 * 1000) { Async = true }; + _timer ??= new TimerX(s => Save(s as DataCache), this, 100, 10 * 60 * 1000) { Async = true }; _timer.SetNext(100); } #endregion diff --git a/XCode/Configuration/DbConfigProvider.cs b/XCode/Configuration/DbConfigProvider.cs index 8a8ef4714..0e7d7d8c8 100644 --- a/XCode/Configuration/DbConfigProvider.cs +++ b/XCode/Configuration/DbConfigProvider.cs @@ -1,5 +1,6 @@ using System.Security.Cryptography; using NewLife; +using NewLife.Common; using NewLife.Configuration; using NewLife.Data; using NewLife.Log; @@ -38,9 +39,21 @@ public override void Init(String? value) // 本地缓存。兼容旧版配置文件 var name = Category; - var file = $"Config/dbConfig_{name}.json".GetFullPath(); - var old = $"Config/{name}.config".GetFullPath(); - if (!File.Exists(file)) file = old; + var path = Path.GetTempPath().CombinePath(SysConfig.Current.Name); + var file = path.CombinePath($"dbConfig_{name}.json").GetFullPath(); + var old = $"Config/dbConfig_{name}.json".GetFullPath(); + if (!File.Exists(file) && File.Exists(old)) + { + try + { + File.Move(old, file); + } + catch + { + file = old; + } + } + if ((Root == null || Root.Childs == null || Root.Childs.Count == 0) && CacheLevel > ConfigCacheLevel.NoCache && File.Exists(file)) { XTrace.WriteLine("[{0}/{1}]加载缓存配置:{2}", Category, UserId, file); @@ -182,7 +195,8 @@ private void SaveCache(IDictionary configs) if (CacheLevel > ConfigCacheLevel.NoCache) { var name = Category; - var file = $"Config/dbConfig_{name}.json".GetFullPath(); + var path = Path.GetTempPath().CombinePath(SysConfig.Current.Name); + var file = path.CombinePath($"dbConfig_{name}.json").GetFullPath(); var txt = configs.ToJson(true); // 加密存储