Version 4.12.2 Added SQLiteCache

This commit is contained in:
ClemensF 2019-07-18 23:08:10 +02:00
parent 85287118a5
commit 0c023abbcf
11 changed files with 755 additions and 8 deletions

View file

@ -0,0 +1,14 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("XAML Map Control SQLiteCache (WPF)")]
[assembly: AssemblyDescription("ObjectCache implementation based on SQLite")]
[assembly: AssemblyProduct("XAML Map Control")]
[assembly: AssemblyCompany("Clemens Fischer")]
[assembly: AssemblyCopyright("Copyright © 2019 Clemens Fischer")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("4.12.2")]
[assembly: AssemblyFileVersion("4.12.2")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]

View file

@ -0,0 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0109C2F0-BA2C-420F-B2CA-DB5B29B1A349}</ProjectGuid>
<OutputType>library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MapControl.Caching</RootNamespace>
<AssemblyName>SQLiteCache.WPF</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>..\..\MapControl.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Data.SQLite, Version=1.0.111.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Data.SQLite.Core.1.0.111.0\lib\net46\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<AppDesigner Include="Properties\" />
<Compile Include="SQLiteCache.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\MapControl.snk">
<Link>MapControl.snk</Link>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\MapControl\WPF\MapControl.WPF.csproj">
<Project>{A204A102-C745-4D65-AEC8-7B96FAEDEF2D}</Project>
<Name>MapControl.WPF</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\System.Data.SQLite.Core.1.0.111.0\build\net46\System.Data.SQLite.Core.targets" Condition="Exists('..\..\packages\System.Data.SQLite.Core.1.0.111.0\build\net46\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\System.Data.SQLite.Core.1.0.111.0\build\net46\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\System.Data.SQLite.Core.1.0.111.0\build\net46\System.Data.SQLite.Core.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -0,0 +1,248 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2019 Clemens Fischer
// 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>
public sealed class SQLiteCache : ObjectCache, IDisposable
{
private readonly SQLiteConnection connection;
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");
}
connection = new SQLiteConnection("Data Source=" + Path.GetFullPath(path));
connection.Open();
using (var command = new SQLiteCommand("create table if not exists items (key text, expiration integer, buffer blob)", connection))
{
command.ExecuteNonQuery();
}
}
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()
{
throw new NotSupportedException("SqLiteCache does not support the ability to enumerate items.");
}
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
{
throw new NotSupportedException("SqLiteCache does not support the ability to create change monitors.");
}
public override long GetCount(string regionName = null)
{
if (regionName != null)
{
throw new NotSupportedException("The parameter regionName must be null.");
}
try
{
using (var command = new SQLiteCommand("select count(*) from items", connection))
{
return (long)command.ExecuteScalar();
}
}
catch (Exception ex)
{
Debug.WriteLine("SqLiteCache: GetCount(): {0}", ex.Message);
}
return 0;
}
public override bool Contains(string key, string regionName = null)
{
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.");
}
try
{
using (var command = new SQLiteCommand("select expiration, buffer from items where key=@key", connection))
{
command.Parameters.AddWithValue("@key", key);
return command.ExecuteReader().Read();
}
}
catch (Exception ex)
{
Debug.WriteLine("SqLiteCache: Get(\"{0}\"): {1}", key, ex.Message);
}
return false;
}
public override object Get(string key, string regionName = null)
{
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.");
}
try
{
using (var command = new SQLiteCommand("select expiration, buffer from items where key=@key", connection))
{
command.Parameters.AddWithValue("@key", key);
var reader = command.ExecuteReader();
if (reader.Read())
{
return new ImageCacheItem
{
Expiration = new DateTime((long)reader["expiration"]),
Buffer = (byte[])reader["buffer"]
};
}
}
}
catch (Exception ex)
{
Debug.WriteLine("SqLiteCache: Get(\"{0}\"): {1}", key, ex.Message);
}
return null;
}
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)
{
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.");
}
var imageCacheItem = value as ImageCacheItem;
if (imageCacheItem == null || imageCacheItem.Buffer == null || imageCacheItem.Buffer.Length == 0)
{
throw new NotSupportedException("The parameter value must be an ImageCacheItem with a non-empty Buffer.");
}
try
{
using (var command = new SQLiteCommand("insert or replace into items (key, expiration, buffer) values (@key, @exp, @buf)", connection))
{
command.Parameters.AddWithValue("@key", key);
command.Parameters.AddWithValue("@exp", imageCacheItem.Expiration.Ticks);
command.Parameters.AddWithValue("@buf", imageCacheItem.Buffer);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Debug.WriteLine("SqLiteCache: Set(\"{0}\"): {1}", key, ex.Message);
}
}
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)
{
throw new NotImplementedException();
}
public void Dispose()
{
connection.Dispose();
}
}
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Data.SQLite.Core" version="1.0.111.0" targetFramework="net48" />
</packages>