2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 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;
|
2018-08-21 23:56:54 +02:00
|
|
|
|
using System.Windows.Media;
|
2019-07-22 19:47:57 +02:00
|
|
|
|
using MapControl.Caching;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
namespace Caching
|
2019-07-18 21:09:54 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
public class ImageCacheItem
|
|
|
|
|
|
{
|
|
|
|
|
|
public byte[] Buffer { get; set; }
|
|
|
|
|
|
public DateTime Expiration { get; set; }
|
|
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public partial class TileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2019-07-18 21:09:54 +02:00
|
|
|
|
/// 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>
|
2019-07-18 21:09:54 +02:00
|
|
|
|
/// 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;
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2019-07-11 18:00:21 +02:00
|
|
|
|
private static async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
var cacheItem = await GetCacheAsync(cacheKey).ConfigureAwait(false);
|
|
|
|
|
|
var buffer = cacheItem?.Buffer;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (response != null) // download succeeded
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
await SetCacheAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (buffer != null && buffer.Length > 0)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-13 19:53:03 +02:00
|
|
|
|
SetTileImageAsync(tile, await ImageLoader.LoadImageAsync(buffer).ConfigureAwait(false));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-11 18:00:21 +02:00
|
|
|
|
private static async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
2018-08-21 23:56:54 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var image = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false);
|
2018-08-21 23:56:54 +02:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
if (image != null)
|
|
|
|
|
|
{
|
2019-07-13 19:53:03 +02:00
|
|
|
|
SetTileImageAsync(tile, image);
|
2019-06-15 01:39:07 +02:00
|
|
|
|
}
|
2018-08-21 23:56:54 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-13 19:53:03 +02:00
|
|
|
|
private static void SetTileImageAsync(Tile tile, ImageSource image)
|
2018-08-21 23:56:54 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-18 21:09:54 +02:00
|
|
|
|
private static Task<ImageCacheItem> GetCacheAsync(string cacheKey)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
return Task.Run(() => Cache.Get(cacheKey) as ImageCacheItem);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-18 21:09:54 +02:00
|
|
|
|
private static Task SetCacheAsync(string cacheKey, byte[] buffer, DateTime expiration)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
var imageCacheItem = new ImageCacheItem
|
2019-07-13 19:53:03 +02:00
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
Buffer = buffer,
|
|
|
|
|
|
Expiration = expiration
|
|
|
|
|
|
};
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-18 21:09:54 +02:00
|
|
|
|
var cacheItemPolicy = new CacheItemPolicy
|
|
|
|
|
|
{
|
|
|
|
|
|
AbsoluteExpiration = expiration
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return Task.Run(() => Cache.Set(cacheKey, imageCacheItem, cacheItemPolicy));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|