2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2021-01-13 21:19:27 +01:00
|
|
|
|
// © 2021 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;
|
2021-06-30 00:04:44 +02:00
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
2021-06-30 17:56:02 +02:00
|
|
|
|
public partial class ImageFileCache : IImageCache
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-16 21:28:55 +02:00
|
|
|
|
public async Task<ImageCacheItem> GetAsync(string key)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-30 00:04:44 +02:00
|
|
|
|
ImageCacheItem imageCacheItem = 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
|
|
|
|
|
|
|
|
|
|
imageCacheItem = new ImageCacheItem
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-30 00:04:44 +02:00
|
|
|
|
Buffer = buffer.AsBuffer(),
|
|
|
|
|
|
Expiration = expiration
|
2019-07-22 19:47:57 +02:00
|
|
|
|
};
|
2021-06-30 00:04:44 +02:00
|
|
|
|
|
2021-06-30 12:18:21 +02:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Read {0}, Expires {1}", path, expiration.ToLocalTime());
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-30 12:18:21 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2021-06-30 00:04:44 +02:00
|
|
|
|
return imageCacheItem;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-16 21:28:55 +02:00
|
|
|
|
public async Task SetAsync(string key, IBuffer 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-01 00:01:10 +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-01 00:01:10 +02:00
|
|
|
|
await stream.AsOutputStream().WriteAsync(buffer);
|
2021-07-01 15:43:52 +02:00
|
|
|
|
await WriteExpirationAsync(stream, expiration);
|
2021-06-30 00:04:44 +02:00
|
|
|
|
}
|
2021-06-30 12:18:21 +02:00
|
|
|
|
|
|
|
|
|
|
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, expiration.ToLocalTime());
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-06-30 12:18:21 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed writing {0}: {1}", path, ex.Message);
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|