mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 06:26:41 +00:00
Version 4.13.1 Cache "no tile" responses.
This commit is contained in:
parent
576dd8e8e7
commit
219171381f
27 changed files with 200 additions and 338 deletions
|
|
@ -25,18 +25,18 @@ namespace MapControl.Caching
|
|||
FileSystemRights.FullControl, AccessControlType.Allow);
|
||||
|
||||
private readonly MemoryCache memoryCache = MemoryCache.Default;
|
||||
private readonly string rootFolder;
|
||||
private readonly string folder;
|
||||
|
||||
public ImageFileCache(string rootFolder)
|
||||
public ImageFileCache(string folder)
|
||||
{
|
||||
if (string.IsNullOrEmpty(rootFolder))
|
||||
if (string.IsNullOrEmpty(folder))
|
||||
{
|
||||
throw new ArgumentException("The parameter rootFolder must not be null or empty.");
|
||||
throw new ArgumentException("The parameter folder must not be null or empty.");
|
||||
}
|
||||
|
||||
this.rootFolder = rootFolder;
|
||||
this.folder = folder;
|
||||
|
||||
Debug.WriteLine("Created ImageFileCache in " + rootFolder);
|
||||
Debug.WriteLine("Created ImageFileCache in " + folder);
|
||||
}
|
||||
|
||||
public override string Name
|
||||
|
|
@ -162,16 +162,16 @@ namespace MapControl.Caching
|
|||
|
||||
var imageCacheItem = value as ImageCacheItem;
|
||||
|
||||
if (imageCacheItem == null || imageCacheItem.Buffer == null || imageCacheItem.Buffer.Length == 0)
|
||||
if (imageCacheItem == null)
|
||||
{
|
||||
throw new ArgumentException("The parameter value must be an ImageCacheItem with a non-empty Buffer.");
|
||||
throw new ArgumentException("The parameter value must be a MapControl.Caching.ImageCacheItem instance.");
|
||||
}
|
||||
|
||||
memoryCache.Set(key, imageCacheItem, policy);
|
||||
|
||||
var path = GetPath(key);
|
||||
string path;
|
||||
|
||||
if (path != null)
|
||||
if (imageCacheItem.Buffer != null && imageCacheItem.Buffer.Length > 0 && (path = GetPath(key)) != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -284,11 +284,11 @@ namespace MapControl.Caching
|
|||
{
|
||||
try
|
||||
{
|
||||
return Path.Combine(rootFolder, Path.Combine(key.Split('\\', '/', ',', ':', ';')));
|
||||
return Path.Combine(folder, Path.Combine(key.Split('\\', '/', ',', ':', ';')));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Invalid key {0}/{1}: {2}", rootFolder, key, ex.Message);
|
||||
Debug.WriteLine("ImageFileCache: Invalid key {0}/{1}: {2}", folder, key, ex.Message);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ using System.Windows;
|
|||
[assembly: AssemblyCompany("Clemens Fischer")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2019 Clemens Fischer")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyVersion("4.13.0")]
|
||||
[assembly: AssemblyFileVersion("4.13.0")]
|
||||
[assembly: AssemblyVersion("4.13.1")]
|
||||
[assembly: AssemblyFileVersion("4.13.1")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
|
|
|
|||
|
|
@ -7,13 +7,17 @@ using System.IO;
|
|||
using System.Runtime.Caching;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
using MapControl.Caching;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class ImageCacheItem
|
||||
namespace Caching
|
||||
{
|
||||
public byte[] Buffer { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
public class ImageCacheItem
|
||||
{
|
||||
public byte[] Buffer { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
|
|
@ -38,22 +42,19 @@ namespace MapControl
|
|||
var cacheItem = await GetCacheAsync(cacheKey).ConfigureAwait(false);
|
||||
var buffer = cacheItem?.Buffer;
|
||||
|
||||
if (buffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
|
||||
|
||||
if (response != null) // download succeeded
|
||||
{
|
||||
buffer = response.Buffer;
|
||||
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
||||
|
||||
if (buffer != null) // tile image available
|
||||
{
|
||||
await SetCacheAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
}
|
||||
await SetCacheAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer != null)
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
SetTileImageAsync(tile, await ImageLoader.LoadImageAsync(buffer).ConfigureAwait(false));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue