2024-08-14 17:27:14 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2025-01-01 18:57:55 +01:00
|
|
|
|
// Copyright © Clemens Fischer
|
2024-08-14 17:27:14 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
2025-02-19 19:48:38 +01:00
|
|
|
|
using System.IO;
|
2024-08-14 17:27:14 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// IDistributedCache implementation based on local image files.
|
|
|
|
|
|
/// </summary>
|
2025-02-19 19:48:38 +01:00
|
|
|
|
public sealed class ImageFileCache : IDistributedCache, IDisposable
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
|
|
|
|
|
private readonly MemoryDistributedCache memoryCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
|
|
|
|
|
|
private readonly StorageFolder rootFolder;
|
2025-02-19 19:48:38 +01:00
|
|
|
|
private readonly Timer cleanTimer;
|
|
|
|
|
|
private bool cleaning;
|
2024-08-14 17:27:14 +02:00
|
|
|
|
|
|
|
|
|
|
public ImageFileCache(StorageFolder folder)
|
2025-02-19 19:48:38 +01:00
|
|
|
|
: this(folder, TimeSpan.FromHours(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ImageFileCache(StorageFolder folder, TimeSpan autoCleanInterval)
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
|
|
|
|
|
rootFolder = folder ?? throw new ArgumentException($"The {nameof(folder)} argument must not be null or empty.", nameof(folder));
|
|
|
|
|
|
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(ImageFileCache)}: {rootFolder.Path}");
|
2024-08-14 17:27:14 +02:00
|
|
|
|
|
2025-02-19 19:48:38 +01:00
|
|
|
|
if (autoCleanInterval > TimeSpan.Zero)
|
|
|
|
|
|
{
|
|
|
|
|
|
cleanTimer = new Timer(_ => CleanAsync().Wait(), null, TimeSpan.Zero, autoCleanInterval);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
cleanTimer?.Dispose();
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] Get(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Set(string key, byte[] buffer, DistributedCacheEntryOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
2025-02-19 19:48:38 +01:00
|
|
|
|
|
2024-08-14 17:27:14 +02:00
|
|
|
|
public void Remove(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task RemoveAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Refresh(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task RefreshAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
var buffer = await memoryCache.GetAsync(key, token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var item = await rootFolder.TryGetItemAsync(key.Replace('/', '\\'));
|
|
|
|
|
|
|
2025-02-19 19:48:38 +01:00
|
|
|
|
if (item is StorageFile file && file.DateCreated > DateTimeOffset.Now)
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
|
|
|
|
|
buffer = (await FileIO.ReadBufferAsync(file)).ToArray();
|
|
|
|
|
|
|
2025-02-19 19:48:38 +01:00
|
|
|
|
var options = new DistributedCacheEntryOptions { AbsoluteExpiration = file.DateCreated };
|
|
|
|
|
|
|
|
|
|
|
|
await memoryCache.SetAsync(key, buffer, options, token).ConfigureAwait(false);
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed reading {key}: {ex.Message}");
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SetAsync(string key, byte[] buffer, DistributedCacheEntryOptions options, CancellationToken token = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
await memoryCache.SetAsync(key, buffer, options, token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer?.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var keyComponents = key.Split('/');
|
|
|
|
|
|
var folder = rootFolder;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < keyComponents.Length - 1; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
folder = await folder.CreateFolderAsync(keyComponents[i], CreationCollisionOption.OpenIfExists);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var file = await folder.CreateFileAsync(keyComponents[keyComponents.Length - 1], CreationCollisionOption.OpenIfExists);
|
|
|
|
|
|
|
2025-02-19 19:48:38 +01:00
|
|
|
|
await FileIO.WriteBytesAsync(file, buffer);
|
2024-08-14 17:27:14 +02:00
|
|
|
|
|
2025-02-19 19:48:38 +01:00
|
|
|
|
var expiration = options.AbsoluteExpiration.HasValue
|
|
|
|
|
|
? options.AbsoluteExpiration.Value.LocalDateTime
|
|
|
|
|
|
: DateTime.Now.Add(options.AbsoluteExpirationRelativeToNow ?? (options.SlidingExpiration ?? TimeSpan.FromDays(1)));
|
|
|
|
|
|
|
|
|
|
|
|
File.SetCreationTime(file.Path, expiration);
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed writing {key}: {ex.Message}");
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task CleanAsync()
|
|
|
|
|
|
{
|
2025-02-19 19:48:38 +01:00
|
|
|
|
if (!cleaning)
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
2025-02-19 19:48:38 +01:00
|
|
|
|
cleaning = true;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var folder in await rootFolder.GetFoldersAsync())
|
|
|
|
|
|
{
|
|
|
|
|
|
var deletedFileCount = await CleanFolder(folder);
|
|
|
|
|
|
|
|
|
|
|
|
if (deletedFileCount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine($"{nameof(ImageFileCache)}: Deleted {deletedFileCount} expired files in {folder.Name}.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cleaning = false;
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-26 21:45:35 +01:00
|
|
|
|
private static async Task<int> CleanFolder(StorageFolder folder)
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
|
|
|
|
|
var deletedFileCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-02-19 19:48:38 +01:00
|
|
|
|
foreach (var subFolder in await folder.GetFoldersAsync())
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
2025-02-19 19:48:38 +01:00
|
|
|
|
deletedFileCount += await CleanFolder(subFolder);
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-19 19:48:38 +01:00
|
|
|
|
foreach (var file in (await folder.GetFilesAsync()).Where(f => f.DateCreated <= DateTime.Now))
|
2024-08-14 17:27:14 +02:00
|
|
|
|
{
|
2025-02-19 19:48:38 +01:00
|
|
|
|
await file.DeleteAsync();
|
|
|
|
|
|
deletedFileCount++;
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((await folder.GetItemsAsync()).Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
await folder.DeleteAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed cleaning {folder.Path}: {ex.Message}");
|
2024-08-14 17:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return deletedFileCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|