2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 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
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ObjectCache implementation based on local image files.
|
2015-11-28 21:09:25 +01:00
|
|
|
|
/// The only valid data type for cached values is byte[].
|
2012-08-15 21:31:10 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ImageFileCache : ObjectCache
|
|
|
|
|
|
{
|
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;
|
|
|
|
|
|
private readonly string rootFolder;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
public ImageFileCache(string rootFolder)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
if (string.IsNullOrEmpty(rootFolder))
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
throw new ArgumentException("The parameter rootFolder must not be null or empty.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
this.rootFolder = rootFolder;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("Created ImageFileCache in " + rootFolder);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
get { return string.Empty; }
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override DefaultCacheCapabilities DefaultCacheCapabilities
|
|
|
|
|
|
{
|
2016-07-03 00:03:38 +02:00
|
|
|
|
get { return DefaultCacheCapabilities.None; }
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object this[string key]
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return Get(key); }
|
|
|
|
|
|
set { Set(key, value, null); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (regionName != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return memoryCache.Contains(key) || FindFile(key) != null;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object Get(string key, string regionName = null)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (regionName != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var buffer = memoryCache.Get(key) as byte[];
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (buffer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = FindFile(key);
|
|
|
|
|
|
|
|
|
|
|
|
if (path != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Reading " + path);
|
2015-11-28 21:09:25 +01:00
|
|
|
|
buffer = File.ReadAllBytes(path);
|
|
|
|
|
|
memoryCache.Set(key, buffer, new CacheItemPolicy());
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2016-07-03 00:03:38 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
return buffer;
|
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)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (regionName != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
var buffer = value as byte[];
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer == null || buffer.Length == 0)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter value must be a non-empty byte array.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
memoryCache.Set(key, buffer, policy);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
var path = GetPath(key);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
if (path != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
//Debug.WriteLine("ImageFileCache: Writing {0}, Expires {1}", path, policy.AbsoluteExpiration.DateTime.ToLocalTime());
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
File.WriteAllBytes(path, buffer);
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
var fileSecurity = File.GetAccessControl(path);
|
|
|
|
|
|
fileSecurity.AddAccessRule(fullControlRule);
|
|
|
|
|
|
File.SetAccessControl(path, fileSecurity);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed writing {0}: {1}", path, ex.Message);
|
|
|
|
|
|
}
|
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)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (regionName != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
memoryCache.Remove(key);
|
|
|
|
|
|
|
|
|
|
|
|
var path = FindFile(key);
|
|
|
|
|
|
|
|
|
|
|
|
if (path != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
}
|
2015-11-28 21:09:25 +01:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2016-07-03 00:03:38 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed removing {0}: {1}", path, ex.Message);
|
2015-11-28 21:09:25 +01:00
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
return null;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
private string FindFile(string key)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed finding {0}: {1}", path, ex.Message);
|
|
|
|
|
|
}
|
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
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
private string GetPath(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
return Path.Combine(rootFolder, Path.Combine(key.Split('\\', '/', ',', ':', ';')));
|
2015-11-28 21:09:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Invalid key {0}/{1}: {2}", rootFolder, key, ex.Message);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|