ImageFileCache trace output

This commit is contained in:
ClemensFischer 2025-05-08 19:51:31 +02:00
parent ad84894b47
commit 1e220b36e2
3 changed files with 33 additions and 14 deletions

View file

@ -90,6 +90,8 @@ namespace MapControl.Caching
var options = new DistributedCacheEntryOptions { AbsoluteExpiration = file.CreationTime };
memoryCache.Set(key, value, options);
logger?.LogTrace("Read {name}", file.FullName);
}
}
catch (Exception ex)
@ -123,6 +125,8 @@ namespace MapControl.Caching
var options = new DistributedCacheEntryOptions { AbsoluteExpiration = file.CreationTime };
await memoryCache.SetAsync(key, value, options, token).ConfigureAwait(false);
logger?.LogTrace("Read {name}", file.FullName);
}
}
catch (Exception ex)
@ -155,6 +159,8 @@ namespace MapControl.Caching
}
SetExpiration(file, options);
logger?.LogTrace("Wrote {name}", file.FullName);
}
}
catch (Exception ex)
@ -184,6 +190,8 @@ namespace MapControl.Caching
}
SetExpiration(file, options);
logger?.LogTrace("Wrote {name}", file.FullName);
}
}
catch (Exception ex)

View file

@ -1,6 +1,4 @@
using System;
using System.Diagnostics;
#if WPF
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

View file

@ -8,6 +8,13 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
#if WPF
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml.Media;
#endif
namespace MapControl
{
@ -127,7 +134,9 @@ namespace MapControl
if (string.IsNullOrEmpty(cacheName))
{
await LoadTileImage(tile, () => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel)).ConfigureAwait(false);
Task<ImageSource> LoadImage() => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel);
await LoadTileImage(tile, LoadImage).ConfigureAwait(false);
}
else
{
@ -139,7 +148,9 @@ namespace MapControl
if (buffer != null && buffer.Length > 0)
{
await LoadTileImage(tile, () => tileSource.LoadImageAsync(buffer)).ConfigureAwait(false);
Task<ImageSource> LoadImage() => tileSource.LoadImageAsync(buffer);
await LoadTileImage(tile, LoadImage).ConfigureAwait(false);
}
}
}
@ -160,7 +171,7 @@ namespace MapControl
try
{
buffer = await Cache.GetAsync(cacheKey);
buffer = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
}
catch (Exception ex)
{
@ -173,18 +184,20 @@ namespace MapControl
if (response != null)
{
buffer = response.Buffer;
buffer = response.Buffer ?? Array.Empty<byte>(); // cache even if null, when no tile available
try
{
var expiration = !response.MaxAge.HasValue ? DefaultCacheExpiration
var options = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow =
!response.MaxAge.HasValue ? DefaultCacheExpiration
: response.MaxAge.Value < MinCacheExpiration ? MinCacheExpiration
: response.MaxAge.Value > MaxCacheExpiration ? MaxCacheExpiration
: response.MaxAge.Value;
: response.MaxAge.Value
};
await Cache.SetAsync(cacheKey,
buffer ?? Array.Empty<byte>(), // cache even if null, when no tile available
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = expiration });
await Cache.SetAsync(cacheKey, buffer, options).ConfigureAwait(false);
}
catch (Exception ex)
{