Version 4.12.2 Improved TileImageLoader.Cache

This commit is contained in:
ClemensF 2019-07-18 21:09:54 +02:00
parent a25cc91c2f
commit 85287118a5
8 changed files with 139 additions and 116 deletions

View file

@ -10,12 +10,13 @@ using System.Linq;
using System.Runtime.Caching;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
namespace MapControl.Caching
{
/// <summary>
/// ObjectCache implementation based on local image files.
/// The only valid data type for cached values is byte[].
/// The only valid data type for cached values is MapControl.ImageCacheItem.
/// </summary>
public class ImageFileCache : ObjectCache
{
@ -96,9 +97,9 @@ namespace MapControl.Caching
throw new NotSupportedException("The parameter regionName must be null.");
}
var buffer = memoryCache.Get(key) as byte[];
var imageCacheItem = memoryCache.Get(key) as ImageCacheItem;
if (buffer == null)
if (imageCacheItem == null)
{
var path = FindFile(key);
@ -106,10 +107,24 @@ namespace MapControl.Caching
{
try
{
//Debug.WriteLine("ImageFileCache: Reading " + path);
var buffer = File.ReadAllBytes(path);
var expiration = DateTime.MinValue;
buffer = File.ReadAllBytes(path);
memoryCache.Set(key, buffer, new CacheItemPolicy());
if (buffer.Length > 16 && Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == "EXPIRES:")
{
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
Array.Resize(ref buffer, buffer.Length - 16);
}
imageCacheItem = new ImageCacheItem
{
Buffer = buffer,
Expiration = expiration
};
memoryCache.Set(key, imageCacheItem, new CacheItemPolicy { AbsoluteExpiration = expiration });
//Debug.WriteLine("ImageFileCache: Reading {0}, Expires {1}", path, imageCacheItem.Expiration.ToLocalTime());
}
catch (Exception ex)
{
@ -118,7 +133,7 @@ namespace MapControl.Caching
}
}
return buffer;
return imageCacheItem;
}
public override CacheItem GetCacheItem(string key, string regionName = null)
@ -145,14 +160,14 @@ namespace MapControl.Caching
throw new NotSupportedException("The parameter regionName must be null.");
}
var buffer = value as byte[];
var imageCacheItem = value as ImageCacheItem;
if (buffer == null || buffer.Length == 0)
if (imageCacheItem == null || imageCacheItem.Buffer == null || imageCacheItem.Buffer.Length == 0)
{
throw new NotSupportedException("The parameter value must be a non-empty byte array.");
throw new NotSupportedException("The parameter value must be an ImageCacheItem with a non-empty Buffer.");
}
memoryCache.Set(key, buffer, policy);
memoryCache.Set(key, imageCacheItem, policy);
var path = GetPath(key);
@ -160,10 +175,16 @@ namespace MapControl.Caching
{
try
{
//Debug.WriteLine("ImageFileCache: Writing {0}, Expires {1}", path, policy.AbsoluteExpiration.DateTime.ToLocalTime());
//Debug.WriteLine("ImageFileCache: Writing {0}, Expires {1}", path, imageCacheItem.Expiration.ToLocalTime());
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllBytes(path, buffer);
using (var stream = File.Create(path))
{
stream.Write(imageCacheItem.Buffer, 0, imageCacheItem.Buffer.Length);
stream.Write(Encoding.ASCII.GetBytes("EXPIRES:"), 0, 8);
stream.Write(BitConverter.GetBytes(imageCacheItem.Expiration.Ticks), 0, 8);
}
var fileSecurity = File.GetAccessControl(path);
fileSecurity.AddAccessRule(fullControlRule);

View file

@ -5,17 +5,21 @@
using System;
using System.IO;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace MapControl
{
public class ImageCacheItem
{
public byte[] Buffer { get; set; }
public DateTime Expiration { get; set; }
}
public partial class TileImageLoader
{
/// <summary>
/// Default folder path where an ObjectCache instance may save cached data,
/// i.e. C:\ProgramData\MapControl\TileCache
/// Default folder path where an ObjectCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache
/// </summary>
public static string DefaultCacheFolder
{
@ -23,17 +27,18 @@ namespace MapControl
}
/// <summary>
/// The ObjectCache used to cache tile images. The default is MemoryCache.Default.
/// An ObjectCache instance used to cache tile image data (i.e. ImageCacheItem objects).
/// The default ObjectCache value is MemoryCache.Default.
/// </summary>
public static ObjectCache Cache { get; set; } = MemoryCache.Default;
private static async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
{
DateTime expiration;
var buffer = GetCachedImage(cacheKey, out expiration);
var cacheItem = await GetCacheAsync(cacheKey).ConfigureAwait(false);
var buffer = cacheItem?.Buffer;
if (buffer == null || expiration < DateTime.UtcNow)
if (buffer == null || cacheItem.Expiration < DateTime.UtcNow)
{
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
@ -43,7 +48,7 @@ namespace MapControl
if (buffer != null) // tile image available
{
await SetCachedImage(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
await SetCacheAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
}
}
}
@ -69,33 +74,25 @@ namespace MapControl
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
}
private static byte[] GetCachedImage(string cacheKey, out DateTime expiration)
private static Task<ImageCacheItem> GetCacheAsync(string cacheKey)
{
var buffer = Cache.Get(cacheKey) as byte[];
if (buffer != null && buffer.Length >= 16 &&
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == "EXPIRES:")
{
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
}
else
{
expiration = DateTime.MinValue;
}
return buffer;
return Task.Run(() => Cache.Get(cacheKey) as ImageCacheItem);
}
private static async Task SetCachedImage(string cacheKey, byte[] buffer, DateTime expiration)
private static Task SetCacheAsync(string cacheKey, byte[] buffer, DateTime expiration)
{
using (var stream = new MemoryStream(buffer.Length + 16))
var imageCacheItem = new ImageCacheItem
{
await stream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
await stream.WriteAsync(Encoding.ASCII.GetBytes("EXPIRES:"), 0, 8).ConfigureAwait(false);
await stream.WriteAsync(BitConverter.GetBytes(expiration.Ticks), 0, 8).ConfigureAwait(false);
Buffer = buffer,
Expiration = expiration
};
Cache.Set(cacheKey, stream.ToArray(), new CacheItemPolicy { AbsoluteExpiration = expiration });
}
var cacheItemPolicy = new CacheItemPolicy
{
AbsoluteExpiration = expiration
};
return Task.Run(() => Cache.Set(cacheKey, imageCacheItem, cacheItemPolicy));
}
}
}