Optimized sorted storage

pull/5045/head
2dust 2024-04-30 14:44:02 +08:00
parent 9d7c7e3225
commit e9b392d1c0
1 changed files with 54 additions and 24 deletions

View File

@ -7,7 +7,7 @@ namespace v2rayN.Handler
internal class ProfileExHandler internal class ProfileExHandler
{ {
private static readonly Lazy<ProfileExHandler> _instance = new(() => new()); private static readonly Lazy<ProfileExHandler> _instance = new(() => new());
private ConcurrentBag<ProfileExItem> _lstProfileEx; private ConcurrentBag<ProfileExItem> _lstProfileEx = [];
private Queue<string> _queIndexIds = new(); private Queue<string> _queIndexIds = new();
public ConcurrentBag<ProfileExItem> ProfileExs => _lstProfileEx; public ConcurrentBag<ProfileExItem> ProfileExs => _lstProfileEx;
public static ProfileExHandler Instance => _instance.Value; public static ProfileExHandler Instance => _instance.Value;
@ -15,6 +15,15 @@ namespace v2rayN.Handler
public ProfileExHandler() public ProfileExHandler()
{ {
Init(); Init();
Task.Run(async () =>
{
while (true)
{
SaveQueueIndexIds();
await Task.Delay(1000 * 600);
}
});
} }
private void Init() private void Init()
@ -22,24 +31,6 @@ namespace v2rayN.Handler
SQLiteHelper.Instance.Execute($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )"); SQLiteHelper.Instance.Execute($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
_lstProfileEx = new(SQLiteHelper.Instance.Table<ProfileExItem>()); _lstProfileEx = new(SQLiteHelper.Instance.Table<ProfileExItem>());
Task.Run(async () =>
{
while (true)
{
var cnt = _queIndexIds.Count;
for (int i = 0; i < cnt; i++)
{
var id = _queIndexIds.Dequeue();
var item = _lstProfileEx.FirstOrDefault(t => t.indexId == id);
if (item is not null)
{
SQLiteHelper.Instance.Replace(item);
}
}
await Task.Delay(1000 * 60);
}
});
} }
private void IndexIdEnqueue(string indexId) private void IndexIdEnqueue(string indexId)
@ -50,6 +41,49 @@ namespace v2rayN.Handler
} }
} }
private void SaveQueueIndexIds()
{
var cnt = _queIndexIds.Count;
if (cnt > 0)
{
var lstExists = SQLiteHelper.Instance.Table<ProfileExItem>();
List<ProfileExItem> lstInserts = [];
List<ProfileExItem> lstUpdates = [];
for (int i = 0; i < cnt; i++)
{
var id = _queIndexIds.Dequeue();
var item = lstExists.FirstOrDefault(t => t.indexId == id);
var itemNew = _lstProfileEx?.FirstOrDefault(t => t.indexId == id);
if (itemNew is null)
{
continue;
}
if (item is not null)
{
lstUpdates.Add(itemNew);
}
else
{
lstInserts.Add(itemNew);
}
}
try
{
if (lstInserts.Count() > 0)
SQLiteHelper.Instance.InsertAll(lstInserts);
if (lstUpdates.Count() > 0)
SQLiteHelper.Instance.UpdateAll(lstUpdates);
}
catch (Exception ex)
{
Logging.SaveLog("ProfileExHandler", ex);
}
}
}
private void AddProfileEx(string indexId, ref ProfileExItem? profileEx) private void AddProfileEx(string indexId, ref ProfileExItem? profileEx)
{ {
profileEx = new() profileEx = new()
@ -73,11 +107,7 @@ namespace v2rayN.Handler
{ {
try try
{ {
//foreach (var item in _lstProfileEx) SaveQueueIndexIds();
//{
// SQLiteHelper.Instance.Replace(item);
//}
SQLiteHelper.Instance.UpdateAll(_lstProfileEx);
} }
catch (Exception ex) catch (Exception ex)
{ {