2019-07-18 23:08:10 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 Clemens Fischer
|
2019-07-18 23:08:10 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Runtime.Caching;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ObjectCache implementation based on SqLite.
|
|
|
|
|
|
/// </summary>
|
2019-07-21 00:17:16 +02:00
|
|
|
|
public partial class SQLiteCache : ObjectCache
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
public SQLiteCache(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("The parameter path must not be null or empty.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Path.GetExtension(path)))
|
|
|
|
|
|
{
|
|
|
|
|
|
path = Path.Combine(path, "TileCache.sqlite");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-21 00:17:16 +02:00
|
|
|
|
connection = Open(Path.GetFullPath(path));
|
2019-07-18 23:08:10 +02:00
|
|
|
|
|
2019-07-21 00:17:16 +02:00
|
|
|
|
Clean();
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return string.Empty; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override DefaultCacheCapabilities DefaultCacheCapabilities
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return DefaultCacheCapabilities.AbsoluteExpirations | DefaultCacheCapabilities.SlidingExpirations; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object this[string key]
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return Get(key); }
|
|
|
|
|
|
set { Set(key, value, null); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new NotSupportedException("SQLiteCache does not support the ability to enumerate items.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new NotSupportedException("SQLiteCache does not support the ability to create change monitors.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long GetCount(string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (regionName != null)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new NotSupportedException("SQLiteCache does not support named regions.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = new SQLiteCommand("select count(*) from items", connection))
|
|
|
|
|
|
{
|
|
|
|
|
|
return (long)command.ExecuteScalar();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
Debug.WriteLine("SQLiteCache.GetCount(): {0}", ex.Message);
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Contains(string key, string regionName = null)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
if (regionName != null)
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new NotSupportedException("SQLiteCache does not support named regions.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-21 00:17:16 +02:00
|
|
|
|
if (key == null)
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
using (var command = GetItemCommand(key))
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
return command.ExecuteReader().Read();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
Debug.WriteLine("SQLiteCache.Contains(\"{0}\"): {1}", key, ex.Message);
|
2019-07-18 23:08:10 +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-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new NotSupportedException("SQLiteCache does not support named regions.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-21 00:17:16 +02:00
|
|
|
|
if (key == null)
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
using (var command = GetItemCommand(key))
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
var reader = command.ExecuteReader();
|
|
|
|
|
|
|
|
|
|
|
|
if (reader.Read())
|
|
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
return new ImageCacheItem
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
Expiration = new DateTime((long)reader["expiration"]),
|
|
|
|
|
|
Buffer = (byte[])reader["buffer"]
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
Debug.WriteLine("SQLiteCache.Get(\"{0}\"): {1}", key, ex.Message);
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
return null;
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new NotSupportedException("SQLiteCache does not support named regions.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-21 00:17:16 +02:00
|
|
|
|
if (key == null)
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var imageCacheItem = value as ImageCacheItem;
|
|
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (imageCacheItem == null)
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
throw new ArgumentException("The parameter value must be a MapControl.Caching.ImageCacheItem instance.");
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
using (var command = SetItemCommand(key, imageCacheItem.Expiration, imageCacheItem.Buffer))
|
2019-07-18 23:08:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
2019-07-21 00:17:16 +02:00
|
|
|
|
|
|
|
|
|
|
//Debug.WriteLine("SQLiteCache.Set(\"{0}\"): expires {1}", key, expiration.ToLocalTime());
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2019-07-21 00:17:16 +02:00
|
|
|
|
Debug.WriteLine("SQLiteCache.Set(\"{0}\"): {1}", key, ex.Message);
|
2019-07-18 23:08:10 +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
|
|
|
|
var oldValue = Get(key, regionName);
|
2019-07-18 23:08:10 +02:00
|
|
|
|
|
2019-07-21 00:17:16 +02:00
|
|
|
|
if (oldValue != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = RemoveItemCommand(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("SQLiteCache.Remove(\"{0}\"): {1}", key, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return oldValue;
|
2019-07-18 23:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|