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,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;
}
}
}

View file

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

View file

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