mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Use IDistributedCache on all platforms
This commit is contained in:
parent
16115413d8
commit
c12e929fcc
39 changed files with 773 additions and 1686 deletions
|
|
@ -1,21 +1,22 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// Copyright © 2023 Clemens Fischer
|
||||
// Copyright © 2024 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
/// <summary>
|
||||
/// Image Cache implementation based on local image files.
|
||||
/// The only valid data type for cached values is Tuple<byte[], DateTime>.
|
||||
/// IDistributedCache implementation based on local image files.
|
||||
/// </summary>
|
||||
public partial class ImageFileCache
|
||||
public class ImageFileCache : IDistributedCache
|
||||
{
|
||||
private const string expiresTag = "EXPIRES:";
|
||||
|
||||
|
|
@ -38,6 +39,155 @@ namespace MapControl.Caching
|
|||
return Task.Factory.StartNew(CleanRootDirectory, TaskCreationOptions.LongRunning);
|
||||
}
|
||||
|
||||
public byte[] Get(string key)
|
||||
{
|
||||
byte[] buffer = null;
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
if (path != null && File.Exists(path))
|
||||
{
|
||||
buffer = File.ReadAllBytes(path);
|
||||
|
||||
CheckExpiration(path, ref buffer);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed reading {path}: {ex.Message}");
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
|
||||
{
|
||||
byte[] buffer = null;
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
if (path != null && File.Exists(path))
|
||||
{
|
||||
#if NETFRAMEWORK
|
||||
using (var stream = File.OpenRead(path))
|
||||
{
|
||||
buffer = new byte[stream.Length];
|
||||
var offset = 0;
|
||||
while (offset < buffer.Length)
|
||||
{
|
||||
offset += await stream.ReadAsync(buffer, offset, buffer.Length - offset, token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
#else
|
||||
buffer = await File.ReadAllBytesAsync(path, token).ConfigureAwait(false);
|
||||
#endif
|
||||
CheckExpiration(path, ref buffer);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed reading {path}: {ex.Message}");
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void Set(string key, byte[] buffer, DistributedCacheEntryOptions options)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
if (path != null && buffer != null && buffer.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
stream.Write(buffer, 0, buffer.Length);
|
||||
|
||||
var expiration = GetExpiration(options);
|
||||
|
||||
if (expiration.HasValue)
|
||||
{
|
||||
stream.Write(Encoding.ASCII.GetBytes(expiresTag), 0, 8);
|
||||
stream.Write(BitConverter.GetBytes(expiration.Value.Ticks), 0, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetAsync(string key, byte[] buffer, DistributedCacheEntryOptions options, CancellationToken token = default)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
if (path != null && buffer != null && buffer.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
await stream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
||||
|
||||
var expiration = GetExpiration(options);
|
||||
|
||||
if (expiration.HasValue)
|
||||
{
|
||||
await stream.WriteAsync(Encoding.ASCII.GetBytes(expiresTag), 0, 8).ConfigureAwait(false);
|
||||
await stream.WriteAsync(BitConverter.GetBytes(expiration.Value.Ticks), 0, 8).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Refresh(string key)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public Task RefreshAsync(string key, CancellationToken token = default)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
if (path != null && File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed deleting {path}: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key, CancellationToken token = default)
|
||||
{
|
||||
Remove(key);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private string GetPath(string key)
|
||||
{
|
||||
try
|
||||
|
|
@ -101,7 +251,9 @@ namespace MapControl.Caching
|
|||
|
||||
try
|
||||
{
|
||||
if (ReadExpiration(file) < DateTime.UtcNow)
|
||||
var expiration = ReadExpiration(file);
|
||||
|
||||
if (expiration.HasValue && expiration.Value <= DateTimeOffset.UtcNow)
|
||||
{
|
||||
file.Delete();
|
||||
deletedFileCount = 1;
|
||||
|
|
@ -115,9 +267,40 @@ namespace MapControl.Caching
|
|||
return deletedFileCount;
|
||||
}
|
||||
|
||||
private static DateTime ReadExpiration(FileInfo file)
|
||||
private static DateTimeOffset? GetExpiration(DistributedCacheEntryOptions options)
|
||||
{
|
||||
DateTime? expiration = null;
|
||||
DateTimeOffset? expiration = null;
|
||||
|
||||
if (options.AbsoluteExpiration.HasValue)
|
||||
{
|
||||
expiration = options.AbsoluteExpiration.Value;
|
||||
}
|
||||
else if (options.AbsoluteExpirationRelativeToNow.HasValue)
|
||||
{
|
||||
expiration = DateTimeOffset.UtcNow.Add(options.AbsoluteExpirationRelativeToNow.Value);
|
||||
}
|
||||
else if (options.SlidingExpiration.HasValue)
|
||||
{
|
||||
expiration = DateTimeOffset.UtcNow.Add(options.SlidingExpiration.Value);
|
||||
}
|
||||
|
||||
return expiration;
|
||||
}
|
||||
|
||||
private static void CheckExpiration(string path, ref byte[] buffer)
|
||||
{
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
if (expiration.HasValue && expiration.Value <= DateTimeOffset.UtcNow)
|
||||
{
|
||||
File.Delete(path);
|
||||
buffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static DateTimeOffset? ReadExpiration(FileInfo file)
|
||||
{
|
||||
DateTimeOffset? expiration = null;
|
||||
|
||||
if (file.Length > 16)
|
||||
{
|
||||
|
|
@ -134,46 +317,32 @@ namespace MapControl.Caching
|
|||
}
|
||||
}
|
||||
|
||||
return expiration ?? DateTime.Today;
|
||||
return expiration;
|
||||
}
|
||||
|
||||
private static DateTime ReadExpiration(ref byte[] buffer)
|
||||
private static DateTimeOffset? ReadExpiration(ref byte[] buffer)
|
||||
{
|
||||
DateTime? expiration = ReadExpiration(buffer);
|
||||
var expiration = ReadExpiration(buffer);
|
||||
|
||||
if (expiration.HasValue)
|
||||
{
|
||||
Array.Resize(ref buffer, buffer.Length - 16);
|
||||
|
||||
return expiration.Value;
|
||||
}
|
||||
|
||||
return DateTime.Today;
|
||||
}
|
||||
|
||||
private static DateTime? ReadExpiration(byte[] buffer)
|
||||
{
|
||||
DateTime? expiration = null;
|
||||
|
||||
if (buffer.Length >= 16 &&
|
||||
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == expiresTag)
|
||||
{
|
||||
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
return expiration;
|
||||
}
|
||||
|
||||
private static void WriteExpiration(Stream stream, DateTime expiration)
|
||||
private static DateTimeOffset? ReadExpiration(byte[] buffer)
|
||||
{
|
||||
stream.Write(Encoding.ASCII.GetBytes(expiresTag), 0, 8);
|
||||
stream.Write(BitConverter.GetBytes(expiration.Ticks), 0, 8);
|
||||
}
|
||||
DateTimeOffset? expiration = null;
|
||||
|
||||
private static async Task WriteExpirationAsync(Stream stream, DateTime expiration)
|
||||
{
|
||||
await stream.WriteAsync(Encoding.ASCII.GetBytes(expiresTag), 0, 8);
|
||||
await stream.WriteAsync(BitConverter.GetBytes(expiration.Ticks), 0, 8);
|
||||
if (buffer.Length >= 16 &&
|
||||
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == expiresTag)
|
||||
{
|
||||
expiration = new DateTimeOffset(BitConverter.ToInt64(buffer, buffer.Length - 8), TimeSpan.Zero);
|
||||
}
|
||||
|
||||
return expiration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// Copyright © 2023 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -39,9 +40,9 @@ namespace MapControl
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maximum number of parallel tile loading tasks. The default value is 4.
|
||||
/// An IDistributedCache implementation used to cache tile images.
|
||||
/// </summary>
|
||||
public static int MaxLoadTasks { get; set; } = 4;
|
||||
public static IDistributedCache Cache { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default expiration time for cached tile images. Used when no expiration time
|
||||
|
|
@ -55,6 +56,12 @@ namespace MapControl
|
|||
/// </summary>
|
||||
public static TimeSpan MaxCacheExpiration { get; set; } = TimeSpan.FromDays(10);
|
||||
|
||||
/// <summary>
|
||||
/// Maximum number of parallel tile loading tasks. The default value is 4.
|
||||
/// </summary>
|
||||
public static int MaxLoadTasks { get; set; } = 4;
|
||||
|
||||
|
||||
private TileQueue pendingTiles;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -125,34 +132,55 @@ namespace MapControl
|
|||
|
||||
if (uri != null)
|
||||
{
|
||||
var extension = Path.GetExtension(uri.LocalPath);
|
||||
|
||||
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
|
||||
{
|
||||
extension = ".jpg";
|
||||
}
|
||||
|
||||
var cacheKey = string.Format(CultureInfo.InvariantCulture,
|
||||
"{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.Column, tile.Row, extension);
|
||||
|
||||
return LoadCachedTileAsync(tile, uri, cacheKey);
|
||||
return LoadCachedTileAsync(tile, uri, cacheName);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static DateTime GetExpiration(TimeSpan? maxAge)
|
||||
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheName)
|
||||
{
|
||||
if (!maxAge.HasValue)
|
||||
var extension = Path.GetExtension(uri.LocalPath);
|
||||
|
||||
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
|
||||
{
|
||||
maxAge = DefaultCacheExpiration;
|
||||
}
|
||||
else if (maxAge.Value > MaxCacheExpiration)
|
||||
{
|
||||
maxAge = MaxCacheExpiration;
|
||||
extension = ".jpg";
|
||||
}
|
||||
|
||||
return DateTime.UtcNow.Add(maxAge.Value);
|
||||
var cacheKey = string.Format(CultureInfo.InvariantCulture,
|
||||
"{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.Column, tile.Row, extension);
|
||||
|
||||
var buffer = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
||||
|
||||
if (buffer == null)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
||||
|
||||
if (response != null) // download succeeded
|
||||
{
|
||||
buffer = response.Buffer ?? Array.Empty<byte>(); // may be empty when no tile available, but still be cached
|
||||
|
||||
var maxAge = response.MaxAge ?? DefaultCacheExpiration;
|
||||
|
||||
if (maxAge > MaxCacheExpiration)
|
||||
{
|
||||
maxAge = MaxCacheExpiration;
|
||||
}
|
||||
|
||||
var cacheOptions = new DistributedCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpiration = DateTimeOffset.UtcNow.Add(maxAge)
|
||||
};
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, cacheOptions).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
//else System.Diagnostics.Debug.WriteLine($"Cached: {cacheKey}");
|
||||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
await LoadTileAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -233,9 +233,6 @@
|
|||
<Compile Include="..\WinUI\GeoImage.WinUI.cs">
|
||||
<Link>GeoImage.WinUI.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\WinUI\ImageFileCache.WinUI.cs">
|
||||
<Link>ImageFileCache.WinUI.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\WinUI\ImageLoader.WinUI.cs">
|
||||
<Link>ImageLoader.WinUI.cs</Link>
|
||||
</Compile>
|
||||
|
|
@ -286,6 +283,9 @@
|
|||
<EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions">
|
||||
<Version>8.0.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
||||
<Version>6.2.14</Version>
|
||||
</PackageReference>
|
||||
|
|
|
|||
|
|
@ -9,16 +9,6 @@ using Windows.UI.Xaml.Media;
|
|||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<Tuple<byte[], DateTime>> GetAsync(string key);
|
||||
|
||||
Task SetAsync(string key, byte[] buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -27,35 +17,6 @@ namespace MapControl
|
|||
/// </summary>
|
||||
public static string DefaultCacheFolder => Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
|
||||
|
||||
/// <summary>
|
||||
/// An IImageCache implementation used to cache tile images.
|
||||
/// </summary>
|
||||
public static Caching.IImageCache Cache { get; set; }
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(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 LoadTileAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,241 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// Copyright © 2023 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Caching;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
public partial class ImageFileCache : ObjectCache
|
||||
{
|
||||
private static readonly FileSystemAccessRule fullControlRule = new FileSystemAccessRule(
|
||||
new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null),
|
||||
FileSystemRights.FullControl, AccessControlType.Allow);
|
||||
|
||||
private readonly MemoryCache memoryCache = MemoryCache.Default;
|
||||
|
||||
public override string Name => string.Empty;
|
||||
|
||||
public override DefaultCacheCapabilities DefaultCacheCapabilities => DefaultCacheCapabilities.None;
|
||||
|
||||
public override object this[string key]
|
||||
{
|
||||
get => Get(key);
|
||||
set => Set(key, value, null);
|
||||
}
|
||||
|
||||
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support the ability to enumerate items.");
|
||||
}
|
||||
|
||||
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support the ability to create change monitors.");
|
||||
}
|
||||
|
||||
public override long GetCount(string regionName = null)
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support the ability to count items.");
|
||||
}
|
||||
|
||||
public override bool Contains(string key, string regionName = null)
|
||||
{
|
||||
if (regionName != null)
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support named regions.");
|
||||
}
|
||||
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (memoryCache.Contains(key))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
return path != null && File.Exists(path);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed finding {path}: {ex.Message}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override object Get(string key, string regionName = null)
|
||||
{
|
||||
if (regionName != null)
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support named regions.");
|
||||
}
|
||||
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
var cacheItem = memoryCache.Get(key) as Tuple<byte[], DateTime>;
|
||||
|
||||
if (cacheItem == null)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
if (path != null && File.Exists(path))
|
||||
{
|
||||
var buffer = File.ReadAllBytes(path);
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
cacheItem = new Tuple<byte[], DateTime>(buffer, expiration);
|
||||
|
||||
memoryCache.Set(key, cacheItem, new CacheItemPolicy { AbsoluteExpiration = expiration });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed reading {path}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
public override CacheItem GetCacheItem(string key, string regionName = null)
|
||||
{
|
||||
var value = Get(key, regionName);
|
||||
|
||||
return value != null ? new CacheItem(key, value) : null;
|
||||
}
|
||||
|
||||
public override IDictionary<string, object> GetValues(IEnumerable<string> keys, string regionName = null)
|
||||
{
|
||||
return keys.ToDictionary(key => key, key => Get(key, regionName));
|
||||
}
|
||||
|
||||
public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
|
||||
{
|
||||
if (regionName != null)
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support named regions.");
|
||||
}
|
||||
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (!(value is Tuple<byte[], DateTime> cacheItem))
|
||||
{
|
||||
throw new ArgumentException("The value argument must be a Tuple<byte[], DateTime>.", nameof(value));
|
||||
}
|
||||
|
||||
memoryCache.Set(key, cacheItem, policy);
|
||||
|
||||
var buffer = cacheItem.Item1;
|
||||
var path = GetPath(key);
|
||||
|
||||
if (buffer != null && buffer.Length > 0 && path != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
stream.Write(buffer, 0, buffer.Length);
|
||||
WriteExpiration(stream, cacheItem.Item2);
|
||||
}
|
||||
|
||||
var fileInfo = new FileInfo(path);
|
||||
var fileSecurity = fileInfo.GetAccessControl();
|
||||
fileSecurity.AddAccessRule(fullControlRule);
|
||||
fileInfo.SetAccessControl(fileSecurity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
|
||||
{
|
||||
Set(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
|
||||
}
|
||||
|
||||
public override void Set(CacheItem item, CacheItemPolicy policy)
|
||||
{
|
||||
Set(item.Key, item.Value, policy, item.RegionName);
|
||||
}
|
||||
|
||||
public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
|
||||
{
|
||||
var oldValue = Get(key, regionName);
|
||||
|
||||
Set(key, value, policy);
|
||||
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
|
||||
{
|
||||
return AddOrGetExisting(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
|
||||
}
|
||||
|
||||
public override CacheItem AddOrGetExisting(CacheItem item, CacheItemPolicy policy)
|
||||
{
|
||||
var oldItem = GetCacheItem(item.Key, item.RegionName);
|
||||
|
||||
Set(item, policy);
|
||||
|
||||
return oldItem;
|
||||
}
|
||||
|
||||
public override object Remove(string key, string regionName = null)
|
||||
{
|
||||
if (regionName != null)
|
||||
{
|
||||
throw new NotSupportedException("ImageFileCache does not support named regions.");
|
||||
}
|
||||
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
memoryCache.Remove(key);
|
||||
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
if (path != null && File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed removing {path}: {ex.Message}");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0-windows;net7.0-windows;net6.0-windows;net48;net462</TargetFrameworks>
|
||||
<TargetFrameworks>net6.0-windows;net462</TargetFrameworks>
|
||||
<UseWPF>true</UseWPF>
|
||||
<RootNamespace>MapControl</RootNamespace>
|
||||
<AssemblyTitle>XAML Map Control Library for WPF</AssemblyTitle>
|
||||
|
|
@ -24,13 +24,12 @@
|
|||
<Compile Include="..\Shared\*.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="$(TargetFramework.EndsWith('windows'))">
|
||||
<PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework.TrimEnd(`2468`))'=='net'">
|
||||
<ItemGroup Condition="'$(TargetFramework)'=='net462'">
|
||||
<Reference Include="System.IO.Compression" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Runtime.Caching" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Caching;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
|
||||
|
|
@ -13,42 +12,11 @@ namespace MapControl
|
|||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// Default folder path where an ObjectCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache
|
||||
/// Default folder path where an IImageCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache
|
||||
/// </summary>
|
||||
public static string DefaultCacheFolder =>
|
||||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
|
||||
|
||||
/// <summary>
|
||||
/// An ObjectCache instance used to cache tile image data. The default value is MemoryCache.Default.
|
||||
/// </summary>
|
||||
public static ObjectCache Cache { get; set; } = MemoryCache.Default;
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
||||
{
|
||||
var cacheItem = Cache.Get(cacheKey) as Tuple<byte[], DateTime>;
|
||||
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
|
||||
|
||||
cacheItem = Tuple.Create(buffer, GetExpiration(response.MaxAge));
|
||||
|
||||
Cache.Set(cacheKey, cacheItem, new CacheItemPolicy { AbsoluteExpiration = cacheItem.Item2 });
|
||||
}
|
||||
}
|
||||
//else System.Diagnostics.Debug.WriteLine($"Cached: {cacheKey}");
|
||||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
await LoadTileAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// Copyright © 2023 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
public partial class ImageFileCache : IImageCache
|
||||
{
|
||||
public async Task<Tuple<byte[], DateTime>> GetAsync(string key)
|
||||
{
|
||||
Tuple<byte[], DateTime> cacheItem = null;
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
{
|
||||
if (path != null && File.Exists(path))
|
||||
{
|
||||
var buffer = await File.ReadAllBytesAsync(path);
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
cacheItem = Tuple.Create(buffer, expiration);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed reading {path}: {ex.Message}");
|
||||
}
|
||||
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
public async Task SetAsync(string key, byte[] buffer, DateTime expiration)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
if (buffer != null && buffer.Length > 0 && path != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
await stream.WriteAsync(buffer, 0, buffer.Length);
|
||||
await WriteExpirationAsync(stream, expiration);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0-windows10.0.17763.0;net7.0-windows10.0.17763.0;net6.0-windows10.0.17763.0</TargetFrameworks>
|
||||
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
|
||||
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
|
||||
<UseRidGraph>true</UseRidGraph>
|
||||
<UseWinUI>true</UseWinUI>
|
||||
|
|
@ -29,5 +29,6 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231219000" />
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -10,16 +10,6 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<Tuple<byte[], DateTime>> GetAsync(string key);
|
||||
|
||||
Task SetAsync(string key, byte[] buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -28,35 +18,6 @@ namespace MapControl
|
|||
public static string DefaultCacheFolder =>
|
||||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
|
||||
|
||||
/// <summary>
|
||||
/// An IImageCache implementation used to cache tile images.
|
||||
/// </summary>
|
||||
public static Caching.IImageCache Cache { get; set; }
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(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 LoadTileAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue