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