// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control // Copyright © 2023 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml.Media; using System; using System.IO; using System.Threading.Tasks; namespace MapControl { namespace Caching { public interface IImageCache { Task> GetAsync(string key); Task SetAsync(string key, byte[] buffer, DateTime expiration); } } public partial class TileImageLoader { /// /// Default folder path where an IImageCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache /// public static string DefaultCacheFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache"); /// /// An IImageCache implementation used to cache tile images. /// public static Caching.IImageCache Cache { get; set; } private static async Task LoadCachedTile(Tile tile, Uri uri, string cacheKey) { var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false); var buffer = cacheItem?.Item1; if (cacheItem == null || cacheItem.Item2 < DateTime.UtcNow) { var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false); if (response != null) // download succeeded { buffer = response.Buffer; // may be null or empty when no tile available, but still be cached await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false); } } //else System.Diagnostics.Debug.WriteLine($"Cached: {cacheKey}"); if (buffer != null && buffer.Length > 0) { await LoadTile(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false); } } private static Task LoadTile(Tile tile, Func> loadImageFunc) { var tcs = new TaskCompletionSource(); async void callback() { try { tile.SetImageSource(await loadImageFunc()); tcs.TrySetResult(); } catch (Exception ex) { tcs.TrySetException(ex); } } if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, callback)) { tile.IsLoaded = false; tcs.TrySetResult(); } return tcs.Task; } } }