Use IDistributedCache on all platforms

This commit is contained in:
ClemensFischer 2024-02-03 20:53:32 +01:00
parent 16115413d8
commit c12e929fcc
39 changed files with 773 additions and 1686 deletions

View file

@ -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}");
}
}
}
}
}

View file

@ -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>

View file

@ -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)
{