2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2017-08-04 21:38:58 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
2021-06-30 17:56:02 +02:00
|
|
|
|
public partial class ImageFileCache : IImageCache
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-07-02 15:57:01 +02:00
|
|
|
|
public async Task<Tuple<byte[], DateTime>> GetAsync(string key)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-07-02 15:57:01 +02:00
|
|
|
|
Tuple<byte[], DateTime> cacheItem = null;
|
2021-06-30 12:18:21 +02:00
|
|
|
|
var path = GetPath(key);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-06-30 12:18:21 +02:00
|
|
|
|
if (path != null && File.Exists(path))
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-30 12:18:21 +02:00
|
|
|
|
var buffer = await File.ReadAllBytesAsync(path);
|
2021-06-30 17:56:02 +02:00
|
|
|
|
var expiration = ReadExpiration(ref buffer);
|
2021-06-30 00:04:44 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
cacheItem = Tuple.Create(buffer, expiration);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-30 12:18:21 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-07-06 18:49:48 +02:00
|
|
|
|
Debug.WriteLine($"ImageFileCache: Failed reading {path}: {ex.Message}");
|
2021-06-30 12:18:21 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2021-07-02 11:35:20 +02:00
|
|
|
|
return cacheItem;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
public async Task SetAsync(string key, byte[] buffer, DateTime expiration)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-07-01 00:01:10 +02:00
|
|
|
|
var path = GetPath(key);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
if (buffer != null && buffer.Length > 0 && path != null)
|
2021-06-30 12:18:21 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
try
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-30 12:18:21 +02:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
2019-07-22 19:47:57 +02:00
|
|
|
|
|
2021-07-01 00:01:10 +02:00
|
|
|
|
using (var stream = File.Create(path))
|
2021-06-30 00:04:44 +02:00
|
|
|
|
{
|
2021-07-02 15:57:01 +02:00
|
|
|
|
await stream.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
|
|
await WriteExpirationAsync(stream, expiration);
|
2021-06-30 00:04:44 +02:00
|
|
|
|
}
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-07-06 18:49:48 +02:00
|
|
|
|
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|