Unified ImageCacheItem

This commit is contained in:
Clemens 2021-07-02 11:35:20 +02:00
parent e138cb83ab
commit 2709f90cdc
12 changed files with 133 additions and 176 deletions

View file

@ -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)
{

View file

@ -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>