Skip to content

Commit d7c3e61

Browse files
committed
Tweaks to caching/prefetching.
1 parent 6fd9914 commit d7c3e61

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

PhotoCull/PhotoCullWindow.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ private async Task reject(bool first) {
231231
photos.Move(goodIndex, 0);
232232
}
233233
good.MarkedForDeletion = false;
234+
reject.Uncache();
234235
this.deleteButton.IsEnabled = true;
235236
this.photoList.SelectedValue = null;
236237
this.prefetch();

PhotoTagger.Imaging/ImageLoadManager.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,29 @@ private async static Task setImage(Photo photo, Photo.Metadata metadata, bool mu
178178
if (photo.Disposed) {
179179
return;
180180
}
181-
var data = await photo.Dispatcher.InvokeAsync(
181+
var (mmap, fullImageStream) = await photo.Dispatcher.InvokeAsync(
182182
() => (photo.mmap, photo.fullImageStream));
183-
if (data.mmap == null) {
183+
if (mmap == null) {
184184
// disposed
185185
return;
186186
}
187-
if (data.fullImageStream == null) {
188-
data.fullImageStream = new UnsafeMemoryMapStream(
189-
data.mmap.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read),
187+
if (fullImageStream == null) {
188+
fullImageStream = new UnsafeMemoryMapStream(
189+
mmap.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read),
190190
FileAccess.Read);
191191
await photo.Dispatcher.InvokeAsync(() => {
192-
var p = Interlocked.CompareExchange(ref photo.fullImageStream, data.fullImageStream, null);
192+
var p = Interlocked.CompareExchange(
193+
ref photo.fullImageStream, fullImageStream, null);
193194
if (p != null) {
194-
data.fullImageStream.Dispose();
195-
data.fullImageStream = p;
195+
fullImageStream.Dispose();
196+
fullImageStream = p;
196197
}
197198
});
198199
}
199200
var img = new BitmapImage();
200201
try {
201202
img.BeginInit();
202-
img.StreamSource = data.fullImageStream.Stream;
203+
img.StreamSource = fullImageStream.Stream;
203204
makeFullImage(metadata, img);
204205
} catch (Exception ex) {
205206
await photo.Dispatcher.InvokeAsync(() => {
@@ -219,6 +220,10 @@ await photo.Dispatcher.InvokeAsync(() => {
219220
photo.loadLock.Release();
220221
}
221222
}
223+
// Now is a good time to do a gen-0 GC to make sure the image we
224+
// just loaded gets promoted to at least gen1 before its caching
225+
// expires.
226+
GC.Collect(0);
222227
}
223228

224229
/// <summary>
@@ -249,7 +254,6 @@ private static void makeFullImage(Photo.Metadata metadata, BitmapImage img) {
249254
}
250255
}
251256
img.CacheOption = BitmapCacheOption.None;
252-
img.CreateOptions = BitmapCreateOptions.DelayCreation;
253257
img.Rotation = Exif.OrienationToRotation(metadata.Orientation);
254258
img.EndInit();
255259
img.Freeze();

PhotoTagger.Imaging/Photo.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,39 @@ public Photo(string f) {
2020
private int fullIsLoading = 0;
2121

2222
private static CacheItemPolicy cachePolicy = new CacheItemPolicy() {
23-
SlidingExpiration = TimeSpan.FromSeconds(10),
23+
SlidingExpiration = TimeSpan.FromSeconds(15),
2424
};
2525

2626
/// <summary>
2727
/// Begin the process of loading the full size image.
2828
/// </summary>
2929
public void Prefetch() {
30+
BitmapImage img = null;
3031
var imageRef = this.fullImageRef;
3132
if (this.setFrom == null ||
3233
this.loader == null) {
3334
// Queue this for prefetch as soon as the metadata is loaded.
3435
Interlocked.CompareExchange(ref this.fullIsLoading, -1, 0);
3536
} else if (!this.Disposed && (
36-
imageRef == null ||
37-
!imageRef.TryGetTarget(out BitmapImage _)) &&
38-
Interlocked.Exchange(ref this.fullIsLoading, 1) <= 0) {
37+
imageRef == null ||
38+
!imageRef.TryGetTarget(out img)) &&
39+
Interlocked.Exchange(ref this.fullIsLoading, 1) <= 0) {
3940
this.loader?.EnqueueFullSizeRead(this, this.setFrom);
41+
} else if (img != null) {
42+
MemoryCache.Default.Set(
43+
this.FileName,
44+
img,
45+
cachePolicy);
4046
}
4147
}
4248

49+
/// <summary>
50+
/// Remove this image from the cache.
51+
/// </summary>
52+
public void Uncache() {
53+
MemoryCache.Default.Remove(this.FileName);
54+
}
55+
4356
public BitmapImage FullImage {
4457
get {
4558
var imageRef = this.fullImageRef;

0 commit comments

Comments
 (0)