@@ -7,41 +7,32 @@ namespace v2rayN.Handler
7
7
internal class ProfileExHandler
8
8
{
9
9
private static readonly Lazy < ProfileExHandler > _instance = new ( ( ) => new ( ) ) ;
10
- private ConcurrentBag < ProfileExItem > _lstProfileEx ;
10
+ private ConcurrentBag < ProfileExItem > _lstProfileEx = [ ] ;
11
11
private Queue < string > _queIndexIds = new ( ) ;
12
12
public ConcurrentBag < ProfileExItem > ProfileExs => _lstProfileEx ;
13
13
public static ProfileExHandler Instance => _instance . Value ;
14
14
15
15
public ProfileExHandler ( )
16
16
{
17
17
Init ( ) ;
18
- }
19
-
20
- private void Init ( )
21
- {
22
- SQLiteHelper . Instance . Execute ( $ "delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )") ;
23
-
24
- _lstProfileEx = new ( SQLiteHelper . Instance . Table < ProfileExItem > ( ) ) ;
25
18
26
19
Task . Run ( async ( ) =>
27
20
{
28
21
while ( true )
29
22
{
30
- var cnt = _queIndexIds . Count ;
31
- for ( int i = 0 ; i < cnt ; i ++ )
32
- {
33
- var id = _queIndexIds . Dequeue ( ) ;
34
- var item = _lstProfileEx . FirstOrDefault ( t => t . indexId == id ) ;
35
- if ( item is not null )
36
- {
37
- SQLiteHelper . Instance . Replace ( item ) ;
38
- }
39
- }
40
- await Task . Delay ( 1000 * 60 ) ;
23
+ SaveQueueIndexIds ( ) ;
24
+ await Task . Delay ( 1000 * 600 ) ;
41
25
}
42
26
} ) ;
43
27
}
44
28
29
+ private void Init ( )
30
+ {
31
+ SQLiteHelper . Instance . Execute ( $ "delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )") ;
32
+
33
+ _lstProfileEx = new ( SQLiteHelper . Instance . Table < ProfileExItem > ( ) ) ;
34
+ }
35
+
45
36
private void IndexIdEnqueue ( string indexId )
46
37
{
47
38
if ( ! Utils . IsNullOrEmpty ( indexId ) && ! _queIndexIds . Contains ( indexId ) )
@@ -50,6 +41,49 @@ private void IndexIdEnqueue(string indexId)
50
41
}
51
42
}
52
43
44
+ private void SaveQueueIndexIds ( )
45
+ {
46
+ var cnt = _queIndexIds . Count ;
47
+ if ( cnt > 0 )
48
+ {
49
+ var lstExists = SQLiteHelper . Instance . Table < ProfileExItem > ( ) ;
50
+ List < ProfileExItem > lstInserts = [ ] ;
51
+ List < ProfileExItem > lstUpdates = [ ] ;
52
+
53
+ for ( int i = 0 ; i < cnt ; i ++ )
54
+ {
55
+ var id = _queIndexIds . Dequeue ( ) ;
56
+ var item = lstExists . FirstOrDefault ( t => t . indexId == id ) ;
57
+ var itemNew = _lstProfileEx ? . FirstOrDefault ( t => t . indexId == id ) ;
58
+ if ( itemNew is null )
59
+ {
60
+ continue ;
61
+ }
62
+
63
+ if ( item is not null )
64
+ {
65
+ lstUpdates . Add ( itemNew ) ;
66
+ }
67
+ else
68
+ {
69
+ lstInserts . Add ( itemNew ) ;
70
+ }
71
+ }
72
+ try
73
+ {
74
+ if ( lstInserts . Count ( ) > 0 )
75
+ SQLiteHelper . Instance . InsertAll ( lstInserts ) ;
76
+
77
+ if ( lstUpdates . Count ( ) > 0 )
78
+ SQLiteHelper . Instance . UpdateAll ( lstUpdates ) ;
79
+ }
80
+ catch ( Exception ex )
81
+ {
82
+ Logging . SaveLog ( "ProfileExHandler" , ex ) ;
83
+ }
84
+ }
85
+ }
86
+
53
87
private void AddProfileEx ( string indexId , ref ProfileExItem ? profileEx )
54
88
{
55
89
profileEx = new ( )
@@ -73,11 +107,7 @@ public void SaveTo()
73
107
{
74
108
try
75
109
{
76
- //foreach (var item in _lstProfileEx)
77
- //{
78
- // SQLiteHelper.Instance.Replace(item);
79
- //}
80
- SQLiteHelper . Instance . UpdateAll ( _lstProfileEx ) ;
110
+ SaveQueueIndexIds ( ) ;
81
111
}
82
112
catch ( Exception ex )
83
113
{
0 commit comments