@@ -19,14 +19,14 @@ public static class ImageLoader
19
19
private static readonly string ClassName = nameof ( ImageLoader ) ;
20
20
21
21
private static readonly ImageCache ImageCache = new ( ) ;
22
- private static SemaphoreSlim storageLock { get ; } = new SemaphoreSlim ( 1 , 1 ) ;
22
+ private static Lock storageLock { get ; } = new ( ) ;
23
23
private static BinaryStorage < List < ( string , bool ) > > _storage ;
24
24
private static readonly ConcurrentDictionary < string , string > GuidToKey = new ( ) ;
25
25
private static IImageHashGenerator _hashGenerator ;
26
26
private static readonly bool EnableImageHash = true ;
27
- public static ImageSource Image { get ; } = new BitmapImage ( new Uri ( Constant . ImageIcon ) ) ;
28
- public static ImageSource MissingImage { get ; } = new BitmapImage ( new Uri ( Constant . MissingImgIcon ) ) ;
29
- public static ImageSource LoadingImage { get ; } = new BitmapImage ( new Uri ( Constant . LoadingImgIcon ) ) ;
27
+ public static ImageSource Image => ImageCache [ Constant . ImageIcon , false ] ;
28
+ public static ImageSource MissingImage => ImageCache [ Constant . MissingImgIcon , false ] ;
29
+ public static ImageSource LoadingImage => ImageCache [ Constant . LoadingImgIcon , false ] ;
30
30
public const int SmallIconSize = 64 ;
31
31
public const int FullIconSize = 256 ;
32
32
public const int FullImageSize = 320 ;
@@ -36,20 +36,25 @@ public static class ImageLoader
36
36
37
37
public static async Task InitializeAsync ( )
38
38
{
39
- _storage = new BinaryStorage < List < ( string , bool ) > > ( "Image" ) ;
40
- _hashGenerator = new ImageHashGenerator ( ) ;
39
+ var usage = await Task . Run ( ( ) =>
40
+ {
41
+ _storage = new BinaryStorage < List < ( string , bool ) > > ( "Image" ) ;
42
+ _hashGenerator = new ImageHashGenerator ( ) ;
41
43
42
- var usage = await LoadStorageToConcurrentDictionaryAsync ( ) ;
43
- _storage . ClearData ( ) ;
44
+ var usage = LoadStorageToConcurrentDictionary ( ) ;
45
+ _storage . ClearData ( ) ;
44
46
45
- ImageCache . Initialize ( usage ) ;
47
+ ImageCache . Initialize ( usage ) ;
46
48
47
- foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . MissingImgIcon } )
48
- {
49
- ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
50
- img . Freeze ( ) ;
51
- ImageCache [ icon , false ] = img ;
52
- }
49
+ foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . ImageIcon , Constant . MissingImgIcon , Constant . LoadingImgIcon } )
50
+ {
51
+ ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
52
+ img . Freeze ( ) ;
53
+ ImageCache [ icon , false ] = img ;
54
+ }
55
+
56
+ return usage ;
57
+ } ) ;
53
58
54
59
_ = Task . Run ( async ( ) =>
55
60
{
@@ -64,42 +69,26 @@ await Stopwatch.InfoAsync(ClassName, "Preload images cost", async () =>
64
69
} ) ;
65
70
}
66
71
67
- public static async Task SaveAsync ( )
72
+ public static void Save ( )
68
73
{
69
- await storageLock . WaitAsync ( ) ;
70
-
71
- try
72
- {
73
- await _storage . SaveAsync ( ImageCache . EnumerateEntries ( )
74
- . Select ( x => x . Key )
75
- . ToList ( ) ) ;
76
- }
77
- catch ( System . Exception e )
74
+ lock ( storageLock )
78
75
{
79
- Log . Exception ( ClassName , "Failed to save image cache to file" , e ) ;
80
- }
81
- finally
82
- {
83
- storageLock . Release ( ) ;
76
+ try
77
+ {
78
+ _storage . Save ( [ .. ImageCache . EnumerateEntries ( ) . Select ( x => x . Key ) ] ) ;
79
+ }
80
+ catch ( System . Exception e )
81
+ {
82
+ Log . Exception ( ClassName , "Failed to save image cache to file" , e ) ;
83
+ }
84
84
}
85
85
}
86
86
87
- public static async Task WaitSaveAsync ( )
87
+ private static List < ( string , bool ) > LoadStorageToConcurrentDictionary ( )
88
88
{
89
- await storageLock . WaitAsync ( ) ;
90
- storageLock . Release ( ) ;
91
- }
92
-
93
- private static async Task < List < ( string , bool ) > > LoadStorageToConcurrentDictionaryAsync ( )
94
- {
95
- await storageLock . WaitAsync ( ) ;
96
- try
97
- {
98
- return await _storage . TryLoadAsync ( new List < ( string , bool ) > ( ) ) ;
99
- }
100
- finally
89
+ lock ( storageLock )
101
90
{
102
- storageLock . Release ( ) ;
91
+ return _storage . TryLoad ( [ ] ) ;
103
92
}
104
93
}
105
94
@@ -174,7 +163,7 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
174
163
Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on first try", e ) ;
175
164
Log . Exception ( ClassName , $ "Failed to get thumbnail for { path } on second try", e2 ) ;
176
165
177
- ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
166
+ ImageSource image = MissingImage ;
178
167
ImageCache [ path , false ] = image ;
179
168
imageResult = new ImageResult ( image , ImageType . Error ) ;
180
169
}
@@ -273,7 +262,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
273
262
}
274
263
else
275
264
{
276
- image = ImageCache [ Constant . MissingImgIcon , false ] ;
265
+ image = MissingImage ;
277
266
path = Constant . MissingImgIcon ;
278
267
}
279
268
0 commit comments