XAML-Map-Control/MapControl/WPF/TileImageLoader.WPF.cs

94 lines
3.2 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.IO;
using System.Runtime.Caching;
using System.Threading.Tasks;
using System.Windows.Media;
using MapControl.Caching;
2017-08-04 21:38:58 +02:00
namespace MapControl
{
namespace Caching
{
public class ImageCacheItem
{
public byte[] Buffer { get; set; }
public DateTime Expiration { get; set; }
}
}
public partial class TileImageLoader
2017-08-04 21:38:58 +02:00
{
/// <summary>
/// Default folder path where an ObjectCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache
2017-08-04 21:38:58 +02:00
/// </summary>
2017-09-06 20:43:46 +02:00
public static string DefaultCacheFolder
{
get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache"); }
}
2017-08-04 21:38:58 +02:00
/// <summary>
/// An ObjectCache instance used to cache tile image data (i.e. ImageCacheItem objects).
/// The default ObjectCache value is MemoryCache.Default.
2017-08-04 21:38:58 +02:00
/// </summary>
public static ObjectCache Cache { get; set; } = MemoryCache.Default;
2020-10-21 22:18:16 +02:00
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
2017-08-04 21:38:58 +02:00
{
var cacheItem = await GetCacheAsync(cacheKey).ConfigureAwait(false);
var buffer = cacheItem?.Buffer;
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
2017-08-04 21:38:58 +02:00
{
2020-09-25 15:02:41 +02:00
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 SetCacheAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
2017-08-04 21:38:58 +02:00
}
}
if (buffer != null && buffer.Length > 0)
2017-08-04 21:38:58 +02:00
{
2020-09-24 23:33:23 +02:00
var image = await ImageLoader.LoadImageAsync(buffer).ConfigureAwait(false);
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
2017-08-04 21:38:58 +02:00
}
}
2020-10-21 22:18:16 +02:00
private static async Task LoadTileAsync(Tile tile, TileSource tileSource)
{
var image = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false);
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
2017-08-04 21:38:58 +02:00
}
private static Task<ImageCacheItem> GetCacheAsync(string cacheKey)
2017-08-04 21:38:58 +02:00
{
return Task.Run(() => Cache.Get(cacheKey) as ImageCacheItem);
2017-08-04 21:38:58 +02:00
}
private static Task SetCacheAsync(string cacheKey, byte[] buffer, DateTime expiration)
2017-08-04 21:38:58 +02:00
{
var imageCacheItem = new ImageCacheItem
{
Buffer = buffer,
Expiration = expiration
};
2017-08-04 21:38:58 +02:00
var cacheItemPolicy = new CacheItemPolicy
{
AbsoluteExpiration = expiration
};
return Task.Run(() => Cache.Set(cacheKey, imageCacheItem, cacheItemPolicy));
2017-08-04 21:38:58 +02:00
}
}
}