TileImageLoader.DefaultCacheFolder

This commit is contained in:
ClemensFischer 2024-08-15 19:46:14 +02:00
parent 2b10cc1d34
commit 1f33cea5f2
7 changed files with 16 additions and 35 deletions

View file

@ -9,12 +9,6 @@ namespace MapControl
{ {
public partial class TileImageLoader public partial class TileImageLoader
{ {
/// <summary>
/// Default folder where the Cache instance may save data, i.e. "C:\ProgramData\MapControl\TileCache".
/// </summary>
public static string DefaultCacheFolder =>
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
private static async Task LoadTileAsync(Tile tile, Func<Task<IImage>> loadImageFunc) private static async Task LoadTileAsync(Tile tile, Func<Task<IImage>> loadImageFunc)
{ {
var image = await loadImageFunc().ConfigureAwait(false); var image = await loadImageFunc().ConfigureAwait(false);

View file

@ -24,18 +24,18 @@ namespace MapControl.Caching
private static readonly byte[] expirationTag = Encoding.ASCII.GetBytes("EXPIRES:"); private static readonly byte[] expirationTag = Encoding.ASCII.GetBytes("EXPIRES:");
private readonly MemoryDistributedCache memoryCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); private readonly MemoryDistributedCache memoryCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
private readonly string rootDirectory; private readonly string rootPath;
public ImageFileCache(string directory) public ImageFileCache(string path)
{ {
if (string.IsNullOrEmpty(directory)) if (string.IsNullOrEmpty(path))
{ {
throw new ArgumentException($"The {nameof(directory)} argument must not be null or empty.", nameof(directory)); throw new ArgumentException($"The {nameof(path)} argument must not be null or empty.", nameof(path));
} }
rootDirectory = directory; rootPath = path;
Debug.WriteLine($"ImageFileCache: {rootDirectory}"); Debug.WriteLine($"ImageFileCache: {rootPath}");
ThreadPool.QueueUserWorkItem(o => Clean()); ThreadPool.QueueUserWorkItem(o => Clean());
} }
@ -210,7 +210,7 @@ namespace MapControl.Caching
public void Clean() public void Clean()
{ {
var deletedFileCount = CleanDirectory(new DirectoryInfo(rootDirectory)); var deletedFileCount = CleanDirectory(new DirectoryInfo(rootPath));
if (deletedFileCount > 0) if (deletedFileCount > 0)
{ {
@ -227,7 +227,7 @@ namespace MapControl.Caching
{ {
try try
{ {
return Path.Combine(rootDirectory, Path.Combine(key.Split('/'))); return Path.Combine(rootPath, Path.Combine(key.Split('/')));
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -44,6 +44,12 @@ namespace MapControl
} }
} }
/// <summary>
/// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache".
/// </summary>
public static string DefaultCacheFolder =>
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
/// <summary> /// <summary>
/// An IDistributedCache implementation used to cache tile images. /// An IDistributedCache implementation used to cache tile images.
/// The default value is a MemoryDistributedCache instance. /// The default value is a MemoryDistributedCache instance.

View file

@ -4,7 +4,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Core; using Windows.UI.Core;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
@ -12,11 +11,6 @@ namespace MapControl
{ {
public partial class TileImageLoader public partial class TileImageLoader
{ {
/// <summary>
/// Default folder where the Cache instance may save data.
/// </summary>
public static StorageFolder DefaultCacheFolder => ApplicationData.Current.LocalCacheFolder;
private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc) private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
{ {
var tcs = new TaskCompletionSource<object>(); var tcs = new TaskCompletionSource<object>();

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media; using System.Windows.Media;
@ -11,12 +10,6 @@ namespace MapControl
{ {
public partial class TileImageLoader public partial class TileImageLoader
{ {
/// <summary>
/// Default folder where the Cache instance may save data, i.e. "C:\ProgramData\MapControl\TileCache".
/// </summary>
public static string DefaultCacheFolder =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc) private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
{ {
var image = await loadImageFunc().ConfigureAwait(false); var image = await loadImageFunc().ConfigureAwait(false);

View file

@ -5,19 +5,12 @@
using Microsoft.UI.Dispatching; using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media;
using System; using System;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MapControl namespace MapControl
{ {
public partial class TileImageLoader public partial class TileImageLoader
{ {
/// <summary>
/// Default folder where the Cache instance may save data, i.e. "C:\ProgramData\MapControl\TileCache".
/// </summary>
public static string DefaultCacheFolder =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
private static Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc) private static Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
{ {
var tcs = new TaskCompletionSource(); var tcs = new TaskCompletionSource();

View file

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Windows.Devices.Input; using Windows.Devices.Input;
using Windows.Storage;
using Windows.System; using Windows.System;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
@ -15,7 +16,7 @@ namespace SampleApplication
{ {
static MainPage() static MainPage()
{ {
//TileImageLoader.Cache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder); //TileImageLoader.Cache = new MapControl.Caching.ImageFileCache(ApplicationData.Current.LocalCacheFolder);
} }
public MainPage() public MainPage()