mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 22:46:58 +00:00
Unified ImageCacheItem
This commit is contained in:
parent
e138cb83ab
commit
2709f90cdc
12 changed files with 133 additions and 176 deletions
|
|
@ -54,53 +54,77 @@ namespace MapControl.Caching
|
|||
|
||||
private void CleanRootDirectory()
|
||||
{
|
||||
foreach (var dir in new DirectoryInfo(rootDirectory).EnumerateDirectories())
|
||||
try
|
||||
{
|
||||
var deletedFileCount = CleanDirectory(dir);
|
||||
|
||||
if (deletedFileCount > 0)
|
||||
foreach (var dir in new DirectoryInfo(rootDirectory).EnumerateDirectories())
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Cleaned {0} files in {1}", deletedFileCount, dir);
|
||||
var deletedFileCount = CleanDirectory(dir);
|
||||
|
||||
if (deletedFileCount > 0)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Cleaned {0} files in {1}", deletedFileCount, dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed enumerating directories in {0}: {1}", rootDirectory, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static int CleanDirectory(DirectoryInfo directory)
|
||||
{
|
||||
var deletedFileCount = 0;
|
||||
|
||||
foreach (var dir in directory.EnumerateDirectories())
|
||||
try
|
||||
{
|
||||
deletedFileCount += CleanDirectory(dir);
|
||||
deletedFileCount += directory.EnumerateDirectories().Sum(dir => CleanDirectory(dir));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed enumerating directories in {0}: {1}", directory.FullName, ex.Message);
|
||||
}
|
||||
|
||||
foreach (var file in directory.EnumerateFiles())
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ReadExpiration(file) < DateTime.UtcNow)
|
||||
{
|
||||
file.Delete();
|
||||
deletedFileCount++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed cleaning {0}: {1}", file.FullName, ex.Message);
|
||||
}
|
||||
deletedFileCount += directory.EnumerateFiles().Sum(file => CleanFile(file));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed enumerating files in {0}: {1}", directory.FullName, ex.Message);
|
||||
}
|
||||
|
||||
if (!directory.EnumerateFileSystemInfos().Any())
|
||||
try
|
||||
{
|
||||
try
|
||||
if (!directory.EnumerateFileSystemInfos().Any())
|
||||
{
|
||||
directory.Delete();
|
||||
}
|
||||
catch (Exception ex)
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed cleaning {0}: {1}", directory.FullName, ex.Message);
|
||||
}
|
||||
|
||||
return deletedFileCount;
|
||||
}
|
||||
|
||||
private static int CleanFile(FileInfo file)
|
||||
{
|
||||
var deletedFileCount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if (ReadExpiration(file) < DateTime.UtcNow)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed cleaning {0}: {1}", directory.FullName, ex.Message);
|
||||
file.Delete();
|
||||
deletedFileCount = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Failed cleaning {0}: {1}", file.FullName, ex.Message);
|
||||
}
|
||||
|
||||
return deletedFileCount;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,15 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public class ImageCacheItem
|
||||
{
|
||||
public byte[] Buffer { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
#if NETFRAMEWORK
|
||||
static class ConcurrentQueueEx
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2021 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage.Streams;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
public class ImageCacheItem
|
||||
{
|
||||
public IBuffer Buffer { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
}
|
||||
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<ImageCacheItem> GetAsync(string key);
|
||||
Task SetAsync(string key, IBuffer buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage.Streams;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
|
|
@ -15,7 +13,7 @@ namespace MapControl.Caching
|
|||
{
|
||||
public async Task<ImageCacheItem> GetAsync(string key)
|
||||
{
|
||||
ImageCacheItem imageCacheItem = null;
|
||||
ImageCacheItem cacheItem = null;
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
|
|
@ -25,9 +23,9 @@ namespace MapControl.Caching
|
|||
var buffer = await File.ReadAllBytesAsync(path);
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
imageCacheItem = new ImageCacheItem
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer.AsBuffer(),
|
||||
Buffer = buffer,
|
||||
Expiration = expiration
|
||||
};
|
||||
|
||||
|
|
@ -39,14 +37,14 @@ namespace MapControl.Caching
|
|||
Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
|
||||
}
|
||||
|
||||
return imageCacheItem;
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
public async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
|
||||
public async Task SetAsync(string key, ImageCacheItem cacheItem)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
if (buffer != null && buffer.Length > 0 && path != null)
|
||||
if (cacheItem.Buffer != null && cacheItem.Buffer.Length > 0 && path != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -54,8 +52,8 @@ namespace MapControl.Caching
|
|||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
await stream.AsOutputStream().WriteAsync(buffer);
|
||||
await WriteExpirationAsync(stream, expiration);
|
||||
await stream.WriteAsync(cacheItem.Buffer, 0, cacheItem.Buffer.Length);
|
||||
await WriteExpirationAsync(stream, cacheItem.Expiration);
|
||||
}
|
||||
|
||||
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, expiration.ToLocalTime());
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
|
|
@ -29,14 +28,16 @@ namespace MapControl
|
|||
return image;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(IBuffer buffer)
|
||||
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await stream.WriteAsync(buffer);
|
||||
stream.Seek(0);
|
||||
return LoadImageAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
return await LoadImageAsync(stream);
|
||||
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||
{
|
||||
using (var stream = new MemoryStream(buffer))
|
||||
{
|
||||
return LoadImageAsync(stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,15 +57,5 @@ namespace MapControl
|
|||
|
||||
return image;
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
||||
{
|
||||
return LoadImageAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||
{
|
||||
return LoadImageAsync(buffer.AsBuffer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -186,7 +186,6 @@
|
|||
<Link>WorldMercatorProjection.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Animatable.UWP.cs" />
|
||||
<Compile Include="ImageCache.UWP.cs" />
|
||||
<Compile Include="ImageFileCache.UWP.cs" />
|
||||
<Compile Include="Map.UWP.cs" />
|
||||
<Compile Include="MapBase.UWP.cs" />
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using MapControl.Caching;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
|
@ -15,6 +15,16 @@ using Microsoft.UI.Xaml.Media;
|
|||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<ImageCacheItem> GetAsync(string key);
|
||||
|
||||
Task SetAsync(string key, ImageCacheItem cacheItem);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -29,7 +39,7 @@ namespace MapControl
|
|||
/// <summary>
|
||||
/// The IImageCache implementation used to cache tile images. The default is null.
|
||||
/// </summary>
|
||||
public static Caching.IImageCache Cache { get; set; }
|
||||
public static IImageCache Cache { get; set; }
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
||||
|
|
@ -43,9 +53,15 @@ namespace MapControl
|
|||
|
||||
if (response != null) // download succeeded
|
||||
{
|
||||
buffer = response.Buffer?.AsBuffer(); // may be null or empty when no tile available, but still be cached
|
||||
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer,
|
||||
Expiration = GetExpiration(response.MaxAge)
|
||||
};
|
||||
|
||||
await Cache.SetAsync(cacheKey, cacheItem).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -95,9 +95,9 @@ namespace MapControl.Caching
|
|||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
var imageCacheItem = memoryCache.Get(key) as ImageCacheItem;
|
||||
var cacheItem = memoryCache.Get(key) as ImageCacheItem;
|
||||
|
||||
if (imageCacheItem == null)
|
||||
if (cacheItem == null)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
|
|
@ -108,15 +108,15 @@ namespace MapControl.Caching
|
|||
var buffer = File.ReadAllBytes(path);
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
imageCacheItem = new ImageCacheItem
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer,
|
||||
Expiration = expiration
|
||||
};
|
||||
|
||||
memoryCache.Set(key, imageCacheItem, new CacheItemPolicy { AbsoluteExpiration = expiration });
|
||||
memoryCache.Set(key, cacheItem, new CacheItemPolicy { AbsoluteExpiration = expiration });
|
||||
|
||||
//Debug.WriteLine("ImageFileCache: Read {0}, Expires {1}", path, imageCacheItem.Expiration.ToLocalTime());
|
||||
//Debug.WriteLine("ImageFileCache: Read {0}, Expires {1}", path, cacheItem.Expiration.ToLocalTime());
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -125,7 +125,7 @@ namespace MapControl.Caching
|
|||
}
|
||||
}
|
||||
|
||||
return imageCacheItem;
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
public override CacheItem GetCacheItem(string key, string regionName = null)
|
||||
|
|
@ -152,17 +152,16 @@ namespace MapControl.Caching
|
|||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (!(value is ImageCacheItem imageCacheItem))
|
||||
if (!(value is ImageCacheItem cacheItem))
|
||||
{
|
||||
throw new ArgumentException("The value argument must be a MapControl.Caching.ImageCacheItem instance.", nameof(value));
|
||||
}
|
||||
|
||||
memoryCache.Set(key, imageCacheItem, policy);
|
||||
memoryCache.Set(key, cacheItem, policy);
|
||||
|
||||
var buffer = imageCacheItem.Buffer;
|
||||
var path = GetPath(key);
|
||||
|
||||
if (buffer != null && buffer.Length > 0 && path != null)
|
||||
if (cacheItem.Buffer != null && cacheItem.Buffer.Length > 0 && path != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -170,8 +169,8 @@ namespace MapControl.Caching
|
|||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
stream.Write(buffer, 0, buffer.Length);
|
||||
WriteExpiration(stream, imageCacheItem.Expiration);
|
||||
stream.Write(cacheItem.Buffer, 0, cacheItem.Buffer.Length);
|
||||
WriteExpiration(stream, cacheItem.Expiration);
|
||||
}
|
||||
|
||||
var fileInfo = new FileInfo(path);
|
||||
|
|
@ -179,7 +178,7 @@ namespace MapControl.Caching
|
|||
fileSecurity.AddAccessRule(fullControlRule);
|
||||
fileInfo.SetAccessControl(fileSecurity);
|
||||
|
||||
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, imageCacheItem.Expiration.ToLocalTime());
|
||||
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, cacheItem.Expiration.ToLocalTime());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,23 +2,14 @@
|
|||
// © 2021 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using MapControl.Caching;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Caching;
|
||||
using System.Threading.Tasks;
|
||||
using MapControl.Caching;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public class ImageCacheItem
|
||||
{
|
||||
public byte[] Buffer { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue