mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Version 2.7.1. Fixed minor issues with caching.
This commit is contained in:
parent
5adcd6568e
commit
ed271719f8
19 changed files with 136 additions and 142 deletions
|
|
@ -11,12 +11,12 @@ namespace MapControl.Caching
|
|||
public class ImageCacheItem
|
||||
{
|
||||
public IBuffer Buffer { get; set; }
|
||||
public DateTime Expires { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
}
|
||||
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<ImageCacheItem> GetAsync(string key);
|
||||
Task SetAsync(string key, IBuffer buffer, DateTime expires);
|
||||
Task SetAsync(string key, IBuffer buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ using System.Windows;
|
|||
[assembly: AssemblyCompany("Clemens Fischer")]
|
||||
[assembly: AssemblyCopyright("© 2015 Clemens Fischer")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyVersion("2.7.0")]
|
||||
[assembly: AssemblyFileVersion("2.7.0")]
|
||||
[assembly: AssemblyVersion("2.7.1")]
|
||||
[assembly: AssemblyFileVersion("2.7.1")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ using System;
|
|||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
|
@ -38,11 +37,16 @@ namespace MapControl
|
|||
|
||||
/// <summary>
|
||||
/// Default expiration time for cached tile images. Used when no expiration time
|
||||
/// was transmitted on download. The default and recommended minimum value is seven days.
|
||||
/// See OpenStreetMap tile usage policy: http://wiki.openstreetmap.org/wiki/Tile_usage_policy
|
||||
/// was transmitted on download. The default value is one day.
|
||||
/// </summary>
|
||||
public static TimeSpan DefaultCacheExpiration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Minimum expiration time for cached tile images. Used when an unnecessarily small expiration time
|
||||
/// was transmitted on download (e.g. Cache-Control: max-age=0). The default value is one hour.
|
||||
/// </summary>
|
||||
public static TimeSpan MinimumCacheExpiration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The ObjectCache used to cache tile images. The default is MemoryCache.Default.
|
||||
/// </summary>
|
||||
|
|
@ -55,7 +59,8 @@ namespace MapControl
|
|||
|
||||
static TileImageLoader()
|
||||
{
|
||||
DefaultCacheExpiration = TimeSpan.FromDays(7);
|
||||
DefaultCacheExpiration = TimeSpan.FromDays(1);
|
||||
MinimumCacheExpiration = TimeSpan.FromHours(1);
|
||||
Cache = MemoryCache.Default;
|
||||
}
|
||||
|
||||
|
|
@ -198,7 +203,7 @@ namespace MapControl
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageTileSource.LoadImage: " + ex.Message);
|
||||
Debug.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
return image;
|
||||
|
|
@ -318,37 +323,33 @@ namespace MapControl
|
|||
memoryStream.Write(BitConverter.GetBytes(expiration.Ticks), 0, 8);
|
||||
|
||||
Cache.Set(cacheKey, memoryStream.ToArray(), new CacheItemPolicy { AbsoluteExpiration = expiration });
|
||||
|
||||
//Debug.WriteLine("Cached {0}, Expires {1}", cacheKey, expiration);
|
||||
}
|
||||
|
||||
private static DateTime GetExpiration(WebHeaderCollection headers)
|
||||
{
|
||||
var expiration = DefaultCacheExpiration;
|
||||
var cacheControl = headers["Cache-Control"];
|
||||
int maxAge;
|
||||
DateTime expiration;
|
||||
|
||||
if (cacheControl != null &&
|
||||
cacheControl.StartsWith("max-age=") &&
|
||||
int.TryParse(cacheControl.Substring(8), out maxAge))
|
||||
if (cacheControl != null)
|
||||
{
|
||||
maxAge = Math.Min(maxAge, (int)DefaultCacheExpiration.TotalSeconds);
|
||||
expiration = DateTime.UtcNow.AddSeconds(maxAge);
|
||||
}
|
||||
else
|
||||
{
|
||||
var expires = headers["Expires"];
|
||||
var maxExpiration = DateTime.UtcNow.Add(DefaultCacheExpiration);
|
||||
int maxAgeValue;
|
||||
var maxAgeDirective = cacheControl
|
||||
.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.FirstOrDefault(s => s.StartsWith("max-age="));
|
||||
|
||||
if (expires == null ||
|
||||
!DateTime.TryParse(expires, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out expiration) ||
|
||||
expiration > maxExpiration)
|
||||
if (maxAgeDirective != null &&
|
||||
int.TryParse(maxAgeDirective.Substring(8), out maxAgeValue))
|
||||
{
|
||||
expiration = maxExpiration;
|
||||
expiration = TimeSpan.FromSeconds(maxAgeValue);
|
||||
|
||||
if (expiration < MinimumCacheExpiration)
|
||||
{
|
||||
expiration = MinimumCacheExpiration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expiration;
|
||||
return DateTime.UtcNow.Add(expiration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,11 +35,16 @@ namespace MapControl
|
|||
|
||||
/// <summary>
|
||||
/// Default expiration time for cached tile images. Used when no expiration time
|
||||
/// was transmitted on download. The default and recommended minimum value is seven days.
|
||||
/// See OpenStreetMap tile usage policy: http://wiki.openstreetmap.org/wiki/Tile_usage_policy
|
||||
/// was transmitted on download. The default value is one day.
|
||||
/// </summary>
|
||||
public static TimeSpan DefaultCacheExpiration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Minimum expiration time for cached tile images. Used when an unnecessarily small expiration time
|
||||
/// was transmitted on download (e.g. Cache-Control: max-age=0). The default value is one hour.
|
||||
/// </summary>
|
||||
public static TimeSpan MinimumCacheExpiration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The IImageCache implementation used to cache tile images. The default is null.
|
||||
/// </summary>
|
||||
|
|
@ -47,7 +52,8 @@ namespace MapControl
|
|||
|
||||
static TileImageLoader()
|
||||
{
|
||||
DefaultCacheExpiration = TimeSpan.FromDays(7);
|
||||
DefaultCacheExpiration = TimeSpan.FromDays(1);
|
||||
MinimumCacheExpiration = TimeSpan.FromHours(1);
|
||||
}
|
||||
|
||||
private class PendingTile
|
||||
|
|
@ -102,7 +108,7 @@ namespace MapControl
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("Loading tile image failed: {0}", ex.Message);
|
||||
Debug.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
var newTaskCount = Math.Min(pendingTiles.Count, tileLayer.MaxParallelDownloads) - taskCount;
|
||||
|
|
@ -143,7 +149,7 @@ namespace MapControl
|
|||
var cacheItem = await Cache.GetAsync(cacheKey);
|
||||
var loaded = false;
|
||||
|
||||
if (cacheItem == null || cacheItem.Expires <= DateTime.UtcNow)
|
||||
if (cacheItem == null || cacheItem.Expiration <= DateTime.UtcNow)
|
||||
{
|
||||
loaded = await DownloadImage(tile, image, uri, cacheKey);
|
||||
}
|
||||
|
|
@ -176,7 +182,7 @@ namespace MapControl
|
|||
return await LoadImageFromHttpResponse(response, tile, image, cacheKey);
|
||||
}
|
||||
|
||||
Debug.WriteLine("{0}: ({1}) {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
Debug.WriteLine("{0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -208,15 +214,19 @@ namespace MapControl
|
|||
stream.Seek(0);
|
||||
await stream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.None);
|
||||
|
||||
var maxAge = DefaultCacheExpiration;
|
||||
var expiration = DefaultCacheExpiration;
|
||||
|
||||
if (response.Headers.CacheControl.MaxAge.HasValue &&
|
||||
response.Headers.CacheControl.MaxAge.Value < maxAge)
|
||||
if (response.Headers.CacheControl.MaxAge.HasValue)
|
||||
{
|
||||
maxAge = response.Headers.CacheControl.MaxAge.Value;
|
||||
expiration = response.Headers.CacheControl.MaxAge.Value;
|
||||
|
||||
if (expiration < MinimumCacheExpiration)
|
||||
{
|
||||
expiration = MinimumCacheExpiration;
|
||||
}
|
||||
}
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, DateTime.UtcNow.Add(maxAge));
|
||||
await Cache.SetAsync(cacheKey, buffer, DateTime.UtcNow.Add(expiration));
|
||||
}
|
||||
|
||||
return loaded;
|
||||
|
|
@ -235,14 +245,12 @@ namespace MapControl
|
|||
{
|
||||
await image.SetSourceAsync(stream);
|
||||
tile.SetImage(image, true, false);
|
||||
|
||||
completion.SetResult(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex.Message);
|
||||
tile.SetImage(null);
|
||||
|
||||
completion.SetResult(false);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ using System.Runtime.InteropServices;
|
|||
[assembly: AssemblyCompany("Clemens Fischer")]
|
||||
[assembly: AssemblyCopyright("© 2015 Clemens Fischer")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyVersion("2.7.0")]
|
||||
[assembly: AssemblyFileVersion("2.7.0")]
|
||||
[assembly: AssemblyVersion("2.7.1")]
|
||||
[assembly: AssemblyFileVersion("2.7.1")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue