Skip to content

Commit 6e2d9e6

Browse files
committed
Avoid double thumbnail cache key resolving
1 parent 3528431 commit 6e2d9e6

3 files changed

Lines changed: 13 additions & 17 deletions

File tree

src/Sdk/SecureFolderFS.Sdk/AppModels/ThumbnailCacheModel.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ public ThumbnailCacheModel(int maxEntries)
4545
/// Tries to get a cached thumbnail for the specified file.
4646
/// The cache key includes the file's modification date, so modified files automatically get new thumbnails.
4747
/// </summary>
48-
/// <param name="file">The file to get the cached thumbnail for.</param>
48+
/// <param name="cacheKey">A unique cache ID.</param>
4949
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param>
5050
/// <returns>A <see cref="Task"/> that represents the asynchronous operation. Value is the cached thumbnail stream if found, otherwise null.</returns>
51-
public async Task<Stream?> TryGetCachedThumbnailAsync(IFile file, CancellationToken cancellationToken = default)
51+
public async Task<Stream?> TryGetCachedThumbnailAsync(string cacheKey, CancellationToken cancellationToken = default)
5252
{
5353
try
5454
{
55-
var cacheKey = await GetCacheKeyAsync(file, cancellationToken);
5655
var cachedData = await _database.GetValueAsync<byte[]>(cacheKey, cancellationToken: cancellationToken);
57-
5856
if (cachedData is null || cachedData.Length == 0)
5957
return null;
6058

@@ -70,16 +68,14 @@ public ThumbnailCacheModel(int maxEntries)
7068
/// Caches the thumbnail for the specified file.
7169
/// The cache key includes the file's modification date, ensuring modified files get fresh thumbnails.
7270
/// </summary>
73-
/// <param name="file">The file to cache the thumbnail for.</param>
71+
/// <param name="cacheKey">A unique cache ID.</param>
7472
/// <param name="thumbnailStream">The thumbnail stream to cache.</param>
7573
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param>
7674
/// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
77-
public async Task CacheThumbnailAsync(IFile file, IImageStream thumbnailStream, CancellationToken cancellationToken = default)
75+
public async Task CacheThumbnailAsync(string cacheKey, IImageStream thumbnailStream, CancellationToken cancellationToken = default)
7876
{
7977
try
8078
{
81-
var cacheKey = await GetCacheKeyAsync(file, cancellationToken);
82-
8379
// Copy thumbnail to byte array
8480
using var memoryStream = new MemoryStream();
8581
await thumbnailStream.CopyToAsync(memoryStream, cancellationToken);
@@ -111,7 +107,7 @@ public Task ClearCacheAsync(CancellationToken cancellationToken = default)
111107
/// <param name="file">The file to generate a cache key for.</param>
112108
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param>
113109
/// <returns>A unique cache key string.</returns>
114-
private static async Task<string> GetCacheKeyAsync(IFile file, CancellationToken cancellationToken)
110+
public static async Task<string> GetCacheKeyAsync(IFile file, CancellationToken cancellationToken)
115111
{
116112
var pathHash = GetPathHash(file.Id);
117113
var dateModified = await file.GetDateModifiedAsync(cancellationToken);

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/Storage/Browser/FileViewModel.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using OwlCore.Storage;
6+
using SecureFolderFS.Sdk.AppModels;
67
using SecureFolderFS.Sdk.Attributes;
78
using SecureFolderFS.Sdk.Enums;
89
using SecureFolderFS.Sdk.Extensions;
@@ -50,14 +51,12 @@ public override async Task InitAsync(CancellationToken cancellationToken = defau
5051
{
5152
Thumbnail?.Dispose();
5253

53-
if (!SettingsService.UserSettings.AreThumbnailsEnabled)
54-
return;
55-
56-
if (!CanLoadThumbnail())
54+
if (!SettingsService.UserSettings.AreThumbnailsEnabled || !CanLoadThumbnail())
5755
return;
5856

5957
// Try to get from the cache first
60-
var cachedStream = await BrowserViewModel.ThumbnailCache.TryGetCachedThumbnailAsync(File, cancellationToken);
58+
var cacheKey = await ThumbnailCacheModel.GetCacheKeyAsync(File, cancellationToken);
59+
var cachedStream = await BrowserViewModel.ThumbnailCache.TryGetCachedThumbnailAsync(cacheKey, cancellationToken);
6160
if (cachedStream is not null)
6261
{
6362
Thumbnail = new StreamImageModel(cachedStream);
@@ -72,7 +71,7 @@ public override async Task InitAsync(CancellationToken cancellationToken = defau
7271

7372
// Show and cache the generated thumbnail
7473
Thumbnail = generatedThumbnail;
75-
_ = BrowserViewModel.ThumbnailCache.CacheThumbnailAsync(File, generatedThumbnail, cancellationToken);
74+
_ = BrowserViewModel.ThumbnailCache.CacheThumbnailAsync(cacheKey, generatedThumbnail, cancellationToken);
7675
}
7776

7877
/// <summary>

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/Storage/Browser/SearchBrowserItemViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public async Task InitAsync(CancellationToken cancellationToken = default)
6767
if (!CanLoadThumbnail() || Inner is not IFile file || Classification is not { TypeHint: var typeHint })
6868
return;
6969

70-
var cachedStream = await _thumbnailCache.TryGetCachedThumbnailAsync(file, cancellationToken).ConfigureAwait(false);
70+
var cacheKey = await ThumbnailCacheModel.GetCacheKeyAsync(file, cancellationToken).ConfigureAwait(false);
71+
var cachedStream = await _thumbnailCache.TryGetCachedThumbnailAsync(cacheKey, cancellationToken).ConfigureAwait(false);
7172
if (cachedStream is not null)
7273
{
7374
await _uiContext.PostOrExecuteAsync(() =>
@@ -89,7 +90,7 @@ await _uiContext.PostOrExecuteAsync(() =>
8990
return Task.CompletedTask;
9091
});
9192

92-
_ = _thumbnailCache.CacheThumbnailAsync(file, generatedThumbnail, cancellationToken);
93+
_ = _thumbnailCache.CacheThumbnailAsync(cacheKey, generatedThumbnail, cancellationToken);
9394
}
9495

9596
public bool CanLoadThumbnail()

0 commit comments

Comments
 (0)