XAML-Map-Control/MapControl/WPF/ImageFileCache.WPF.cs

248 lines
7.8 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using System.Security.AccessControl;
using System.Security.Principal;
namespace MapControl.Caching
{
2021-06-30 17:56:02 +02:00
public partial class ImageFileCache : ObjectCache
{
private static readonly FileSystemAccessRule fullControlRule = new FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null),
FileSystemRights.FullControl, AccessControlType.Allow);
private readonly MemoryCache memoryCache = MemoryCache.Default;
public override string Name
{
2017-08-04 21:38:58 +02:00
get { return string.Empty; }
}
public override DefaultCacheCapabilities DefaultCacheCapabilities
{
get { return DefaultCacheCapabilities.None; }
}
public override object this[string key]
{
get { return Get(key); }
set { Set(key, value, null); }
}
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
throw new NotSupportedException("ImageFileCache does not support the ability to enumerate items.");
}
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
{
throw new NotSupportedException("ImageFileCache does not support the ability to create change monitors.");
}
public override long GetCount(string regionName = null)
{
throw new NotSupportedException("ImageFileCache does not support the ability to count items.");
}
public override bool Contains(string key, string regionName = null)
{
2019-07-21 00:17:16 +02:00
if (regionName != null)
{
2019-07-21 00:17:16 +02:00
throw new NotSupportedException("ImageFileCache does not support named regions.");
}
2019-07-21 00:17:16 +02:00
if (key == null)
{
2021-02-11 23:34:37 +01:00
throw new ArgumentNullException(nameof(key));
}
2021-06-30 20:59:38 +02:00
if (memoryCache.Contains(key))
{
return true;
}
var path = GetPath(key);
try
{
return path != null && File.Exists(path);
}
catch (Exception ex)
{
Debug.WriteLine($"ImageFileCache: Failed finding {path}: {ex.Message}");
2021-06-30 20:59:38 +02:00
}
return false;
}
public override object Get(string key, string regionName = null)
{
2019-07-21 00:17:16 +02:00
if (regionName != null)
{
2019-07-21 00:17:16 +02:00
throw new NotSupportedException("ImageFileCache does not support named regions.");
}
2019-07-21 00:17:16 +02:00
if (key == null)
{
2021-02-11 23:34:37 +01:00
throw new ArgumentNullException(nameof(key));
}
2021-07-02 15:57:01 +02:00
var cacheItem = memoryCache.Get(key) as Tuple<byte[], DateTime>;
2021-07-02 11:35:20 +02:00
if (cacheItem == null)
{
2021-06-30 20:59:38 +02:00
var path = GetPath(key);
2021-06-30 20:59:38 +02:00
try
{
2021-06-30 20:59:38 +02:00
if (path != null && File.Exists(path))
{
var buffer = File.ReadAllBytes(path);
2021-06-30 17:56:02 +02:00
var expiration = ReadExpiration(ref buffer);
2021-07-02 15:57:01 +02:00
cacheItem = new Tuple<byte[], DateTime>(buffer, expiration);
2021-07-02 11:35:20 +02:00
memoryCache.Set(key, cacheItem, new CacheItemPolicy { AbsoluteExpiration = expiration });
}
2021-06-30 20:59:38 +02:00
}
catch (Exception ex)
{
Debug.WriteLine($"ImageFileCache: Failed reading {path}: {ex.Message}");
}
}
2021-07-02 11:35:20 +02:00
return cacheItem;
}
public override CacheItem GetCacheItem(string key, string regionName = null)
{
var value = Get(key, regionName);
return value != null ? new CacheItem(key, value) : null;
}
public override IDictionary<string, object> GetValues(IEnumerable<string> keys, string regionName = null)
{
return keys.ToDictionary(key => key, key => Get(key, regionName));
}
public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
{
2019-07-21 00:17:16 +02:00
if (regionName != null)
{
2019-07-21 00:17:16 +02:00
throw new NotSupportedException("ImageFileCache does not support named regions.");
}
2019-07-21 00:17:16 +02:00
if (key == null)
{
2021-02-11 23:34:37 +01:00
throw new ArgumentNullException(nameof(key));
}
2021-07-02 15:57:01 +02:00
if (!(value is Tuple<byte[], DateTime> cacheItem))
{
2021-07-02 15:57:01 +02:00
throw new ArgumentException("The value argument must be a Tuple<byte[], DateTime>.", nameof(value));
}
2021-07-02 11:35:20 +02:00
memoryCache.Set(key, cacheItem, policy);
2021-07-02 15:57:01 +02:00
var buffer = cacheItem.Item1;
2021-07-01 00:01:10 +02:00
var path = GetPath(key);
2021-07-02 15:57:01 +02:00
if (buffer != null && buffer.Length > 0 && path != null)
{
2017-08-04 21:38:58 +02:00
try
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (var stream = File.Create(path))
{
2021-07-02 15:57:01 +02:00
stream.Write(buffer, 0, buffer.Length);
WriteExpiration(stream, cacheItem.Item2);
}
var fileInfo = new FileInfo(path);
var fileSecurity = fileInfo.GetAccessControl();
2017-08-04 21:38:58 +02:00
fileSecurity.AddAccessRule(fullControlRule);
fileInfo.SetAccessControl(fileSecurity);
2017-08-04 21:38:58 +02:00
}
catch (Exception ex)
{
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
2017-08-04 21:38:58 +02:00
}
}
}
public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
Set(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
}
public override void Set(CacheItem item, CacheItemPolicy policy)
{
Set(item.Key, item.Value, policy, item.RegionName);
}
public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
{
var oldValue = Get(key, regionName);
Set(key, value, policy);
return oldValue;
}
public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
return AddOrGetExisting(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
}
public override CacheItem AddOrGetExisting(CacheItem item, CacheItemPolicy policy)
{
var oldItem = GetCacheItem(item.Key, item.RegionName);
Set(item, policy);
return oldItem;
}
public override object Remove(string key, string regionName = null)
{
2019-07-21 00:17:16 +02:00
if (regionName != null)
{
2019-07-21 00:17:16 +02:00
throw new NotSupportedException("ImageFileCache does not support named regions.");
}
2019-07-21 00:17:16 +02:00
if (key == null)
{
2021-02-11 23:34:37 +01:00
throw new ArgumentNullException(nameof(key));
}
memoryCache.Remove(key);
2017-08-04 21:38:58 +02:00
var path = GetPath(key);
try
{
2017-08-04 21:38:58 +02:00
if (path != null && File.Exists(path))
{
2021-06-30 20:59:38 +02:00
File.Delete(path);
}
2017-08-04 21:38:58 +02:00
}
catch (Exception ex)
{
Debug.WriteLine($"ImageFileCache: Failed removing {path}: {ex.Message}");
2017-08-04 21:38:58 +02:00
}
2017-08-04 21:38:58 +02:00
return null;
}
}
}