Skip to content

Commit

Permalink
缓存文件从Config目录转移到临时目录
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 16, 2024
1 parent c055ae8 commit 9edb81b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
37 changes: 34 additions & 3 deletions XCode/Cache/DataCache.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,10 +12,9 @@ namespace XCode.Cache;
public class DataCache
{
#region 静态
private static readonly String _File = @"Config\DataCache.config";
private static DataCache? _Current;
/// <summary>当前实例</summary>
public static DataCache Current => _Current ??= Load(_File.GetBasePath(), true)!;
public static DataCache Current => _Current ??= Load();
#endregion

#region 属性
Expand All @@ -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);
}

/// <summary>加载</summary>
/// <param name="file"></param>
/// <param name="create"></param>
Expand Down Expand Up @@ -67,7 +98,7 @@ public static void Save(String file, DataCache data)
/// <summary>异步保存</summary>
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
Expand Down
22 changes: 18 additions & 4 deletions XCode/Configuration/DbConfigProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Security.Cryptography;
using NewLife;
using NewLife.Common;
using NewLife.Configuration;
using NewLife.Data;
using NewLife.Log;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -182,7 +195,8 @@ private void SaveCache(IDictionary<String, Object?> 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);

// 加密存储
Expand Down

0 comments on commit 9edb81b

Please sign in to comment.