mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Replaced ImageCacheItem by Tuple
This commit is contained in:
parent
2709f90cdc
commit
77c2169999
14 changed files with 60 additions and 122 deletions
|
|
@ -11,9 +11,9 @@ namespace MapControl.Caching
|
|||
{
|
||||
public partial class ImageFileCache : IImageCache
|
||||
{
|
||||
public async Task<ImageCacheItem> GetAsync(string key)
|
||||
public async Task<Tuple<byte[], DateTime>> GetAsync(string key)
|
||||
{
|
||||
ImageCacheItem cacheItem = null;
|
||||
Tuple<byte[], DateTime> cacheItem = null;
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
|
|
@ -23,13 +23,7 @@ namespace MapControl.Caching
|
|||
var buffer = await File.ReadAllBytesAsync(path);
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer,
|
||||
Expiration = expiration
|
||||
};
|
||||
|
||||
//Debug.WriteLine("ImageFileCache: Read {0}, Expires {1}", path, expiration.ToLocalTime());
|
||||
cacheItem = Tuple.Create(buffer, expiration);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -40,11 +34,11 @@ namespace MapControl.Caching
|
|||
return cacheItem;
|
||||
}
|
||||
|
||||
public async Task SetAsync(string key, ImageCacheItem cacheItem)
|
||||
public async Task SetAsync(string key, byte[] buffer, DateTime expiration)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
if (cacheItem.Buffer != null && cacheItem.Buffer.Length > 0 && path != null)
|
||||
if (buffer != null && buffer.Length > 0 && path != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -52,11 +46,9 @@ namespace MapControl.Caching
|
|||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
await stream.WriteAsync(cacheItem.Buffer, 0, cacheItem.Buffer.Length);
|
||||
await WriteExpirationAsync(stream, cacheItem.Expiration);
|
||||
await stream.WriteAsync(buffer, 0, buffer.Length);
|
||||
await WriteExpirationAsync(stream, expiration);
|
||||
}
|
||||
|
||||
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, expiration.ToLocalTime());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MapControl.Caching;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
|
@ -19,9 +18,9 @@ namespace MapControl
|
|||
{
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<ImageCacheItem> GetAsync(string key);
|
||||
Task<Tuple<byte[], DateTime>> GetAsync(string key);
|
||||
|
||||
Task SetAsync(string key, ImageCacheItem cacheItem);
|
||||
Task SetAsync(string key, byte[] buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -39,15 +38,15 @@ namespace MapControl
|
|||
/// <summary>
|
||||
/// The IImageCache implementation used to cache tile images. The default is null.
|
||||
/// </summary>
|
||||
public static IImageCache Cache { get; set; }
|
||||
public static Caching.IImageCache Cache { get; set; }
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
||||
{
|
||||
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
||||
var buffer = cacheItem?.Buffer;
|
||||
var buffer = cacheItem?.Item1;
|
||||
|
||||
if (cacheItem == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||
if (cacheItem == null || cacheItem.Item2 < DateTime.UtcNow)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
||||
|
||||
|
|
@ -55,15 +54,10 @@ namespace MapControl
|
|||
{
|
||||
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
||||
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer,
|
||||
Expiration = GetExpiration(response.MaxAge)
|
||||
};
|
||||
|
||||
await Cache.SetAsync(cacheKey, cacheItem).ConfigureAwait(false);
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else System.Diagnostics.Debug.WriteLine("Cached: " + cacheKey);
|
||||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue