@@ -13,12 +13,11 @@ namespace Flow.Launcher.Infrastructure.Image
13
13
{
14
14
public static class ImageLoader
15
15
{
16
- private static readonly ImageCache _imageCache = new ImageCache ( ) ;
17
- private static readonly ConcurrentDictionary < string , string > _guidToKey = new ConcurrentDictionary < string , string > ( ) ;
18
- private static readonly bool _enableHashImage = true ;
19
-
16
+ private static readonly ImageCache ImageCache = new ImageCache ( ) ;
20
17
private static BinaryStorage < Dictionary < string , int > > _storage ;
18
+ private static readonly ConcurrentDictionary < string , string > GuidToKey = new ConcurrentDictionary < string , string > ( ) ;
21
19
private static IImageHashGenerator _hashGenerator ;
20
+ private static bool EnableImageHash = true ;
22
21
23
22
private static readonly string [ ] ImageExtensions =
24
23
{
@@ -36,39 +35,39 @@ public static void Initialize()
36
35
_storage = new BinaryStorage < Dictionary < string , int > > ( "Image" ) ;
37
36
_hashGenerator = new ImageHashGenerator ( ) ;
38
37
39
- _imageCache . Usage = LoadStorageToConcurrentDictionary ( ) ;
38
+ var usage = LoadStorageToConcurrentDictionary ( ) ;
40
39
41
40
foreach ( var icon in new [ ] { Constant . DefaultIcon , Constant . MissingImgIcon } )
42
41
{
43
42
ImageSource img = new BitmapImage ( new Uri ( icon ) ) ;
44
43
img . Freeze ( ) ;
45
- _imageCache [ icon ] = img ;
44
+ ImageCache [ icon ] = img ;
46
45
}
47
46
48
47
Task . Run ( ( ) =>
49
48
{
50
49
Stopwatch . Normal ( "|ImageLoader.Initialize|Preload images cost" , ( ) =>
51
50
{
52
- _imageCache . Usage . AsParallel ( ) . ForAll ( x =>
51
+ ImageCache . Data . AsParallel ( ) . ForAll ( x =>
53
52
{
54
53
Load ( x . Key ) ;
55
54
} ) ;
56
55
} ) ;
57
- Log . Info ( $ "|ImageLoader.Initialize|Number of preload images is <{ _imageCache . Usage . Count } >, Images Number: { _imageCache . CacheSize ( ) } , Unique Items { _imageCache . UniqueImagesInCache ( ) } ") ;
56
+ Log . Info ( $ "|ImageLoader.Initialize|Number of preload images is <{ ImageCache . CacheSize ( ) } >, Images Number: { ImageCache . CacheSize ( ) } , Unique Items { ImageCache . UniqueImagesInCache ( ) } ") ;
58
57
} ) ;
59
58
}
60
59
61
60
public static void Save ( )
62
61
{
63
62
lock ( _storage )
64
63
{
65
- _storage . Save ( _imageCache . CleanupAndToDictionary ( ) ) ;
64
+ _storage . Save ( ImageCache . Data . Select ( x => ( x . Key , x . Value . usage ) ) . ToDictionary ( x => x . Key , y => y . usage ) ) ;
66
65
}
67
66
}
68
67
69
68
private static ConcurrentDictionary < string , int > LoadStorageToConcurrentDictionary ( )
70
69
{
71
- lock ( _storage )
70
+ lock ( _storage )
72
71
{
73
72
var loaded = _storage . TryLoad ( new Dictionary < string , int > ( ) ) ;
74
73
@@ -106,11 +105,11 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)
106
105
{
107
106
if ( string . IsNullOrEmpty ( path ) )
108
107
{
109
- return new ImageResult ( _imageCache [ Constant . MissingImgIcon ] , ImageType . Error ) ;
108
+ return new ImageResult ( ImageCache [ Constant . MissingImgIcon ] , ImageType . Error ) ;
110
109
}
111
- if ( _imageCache . ContainsKey ( path ) )
110
+ if ( ImageCache . ContainsKey ( path ) )
112
111
{
113
- return new ImageResult ( _imageCache [ path ] , ImageType . Cache ) ;
112
+ return new ImageResult ( ImageCache [ path ] , ImageType . Cache ) ;
114
113
}
115
114
116
115
if ( path . StartsWith ( "data:" , StringComparison . OrdinalIgnoreCase ) )
@@ -139,8 +138,8 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)
139
138
Log . Exception ( $ "|ImageLoader.Load|Failed to get thumbnail for { path } on first try", e ) ;
140
139
Log . Exception ( $ "|ImageLoader.Load|Failed to get thumbnail for { path } on second try", e2 ) ;
141
140
142
- ImageSource image = _imageCache [ Constant . MissingImgIcon ] ;
143
- _imageCache [ path ] = image ;
141
+ ImageSource image = ImageCache [ Constant . MissingImgIcon ] ;
142
+ ImageCache [ path ] = image ;
144
143
imageResult = new ImageResult ( image , ImageType . Error ) ;
145
144
}
146
145
}
@@ -191,7 +190,7 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
191
190
}
192
191
else
193
192
{
194
- image = _imageCache [ Constant . MissingImgIcon ] ;
193
+ image = ImageCache [ Constant . MissingImgIcon ] ;
195
194
path = Constant . MissingImgIcon ;
196
195
}
197
196
@@ -218,27 +217,26 @@ public static ImageSource Load(string path, bool loadFullImage = false)
218
217
219
218
var img = imageResult . ImageSource ;
220
219
if ( imageResult . ImageType != ImageType . Error && imageResult . ImageType != ImageType . Cache )
221
- {
222
- // we need to get image hash
223
- string hash = _enableHashImage ? _hashGenerator . GetHashFromImage ( img ) : null ;
220
+ { // we need to get image hash
221
+ string hash = EnableImageHash ? _hashGenerator . GetHashFromImage ( img ) : null ;
224
222
if ( hash != null )
225
223
{
226
- if ( _guidToKey . TryGetValue ( hash , out string key ) )
227
- {
228
- // image already exists
229
- img = _imageCache [ key ] ;
224
+
225
+ if ( GuidToKey . TryGetValue ( hash , out string key ) )
226
+ { // image already exists
227
+ img = ImageCache [ key ] ?? img ;
230
228
}
231
229
else
232
- {
233
- // new guid
234
- _guidToKey [ hash ] = path ;
230
+ { // new guid
231
+ GuidToKey [ hash ] = path ;
235
232
}
236
233
}
237
234
238
235
// update cache
239
- _imageCache [ path ] = img ;
236
+ ImageCache [ path ] = img ;
240
237
}
241
238
239
+
242
240
return img ;
243
241
}
244
242
0 commit comments