Version 4.13.0

This commit is contained in:
ClemensF 2019-07-21 00:17:16 +02:00
parent 8b08d721d4
commit 8244ddb5c3
13 changed files with 625 additions and 739 deletions

View file

@ -2,7 +2,6 @@
// © 2019 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Microsoft.Data.Sqlite;
using System;
using System.Diagnostics;
using System.IO;
@ -16,10 +15,8 @@ namespace MapControl.Caching
/// <summary>
/// IImageCache implementation based on SqLite.
/// </summary>
public sealed class SQLiteCache : IImageCache, IDisposable
public partial class SQLiteCache : IImageCache
{
private readonly SqliteConnection connection;
public SQLiteCache(StorageFolder folder, string fileName = "TileCache.sqlite")
{
if (folder == null)
@ -32,33 +29,29 @@ namespace MapControl.Caching
throw new ArgumentNullException("The parameter fileName must not be null.");
}
connection = new SqliteConnection("Data Source=" + Path.Combine(folder.Path, fileName));
connection = Open(Path.Combine(folder.Path, fileName));
connection.Open();
using (var command = new SqliteCommand("create table if not exists items (key text, expiration integer, buffer blob)", connection))
{
command.ExecuteNonQuery();
}
}
public void Dispose()
{
connection.Dispose();
Clean();
}
public async Task<ImageCacheItem> GetAsync(string key)
{
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
ImageCacheItem imageCacheItem = null;
try
{
using (var command = new SqliteCommand("select expiration, buffer from items where key=@key", connection))
using (var command = GetItemCommand(key))
{
command.Parameters.AddWithValue("@key", key);
var reader = await command.ExecuteReaderAsync();
if (reader.Read())
if (await reader.ReadAsync())
{
return new ImageCacheItem
imageCacheItem = new ImageCacheItem
{
Expiration = new DateTime((long)reader["expiration"]),
Buffer = ((byte[])reader["buffer"]).AsBuffer()
@ -68,27 +61,36 @@ namespace MapControl.Caching
}
catch (Exception ex)
{
Debug.WriteLine("SqLiteCache: GetAsync(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.GetAsync(\"{0}\"): {1}", key, ex.Message);
}
return null;
return imageCacheItem;
}
public async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
{
if (key == null)
{
throw new ArgumentNullException("The parameter key must not be null.");
}
if (buffer == null)
{
throw new ArgumentNullException("The parameter buffer must not be null.");
}
try
{
using (var command = new SqliteCommand("insert or replace into items (key, expiration, buffer) values (@key, @exp, @buf)", connection))
using (var command = SetItemCommand(key, expiration, buffer.ToArray()))
{
command.Parameters.AddWithValue("@key", key);
command.Parameters.AddWithValue("@exp", expiration.Ticks);
command.Parameters.AddWithValue("@buf", buffer.ToArray());
await command.ExecuteNonQueryAsync();
}
//Debug.WriteLine("SQLiteCache.SetAsync(\"{0}\"): expires {1}", key, expiration.ToLocalTime());
}
catch (Exception ex)
{
Debug.WriteLine("SqLiteCache: SetAsync(\"{0}\"): {1}", key, ex.Message);
Debug.WriteLine("SQLiteCache.SetAsync(\"{0}\"): {1}", key, ex.Message);
}
}
}

View file

@ -40,8 +40,11 @@
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Shared\SQLiteCache.cs">
<Link>SQLiteCache.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SQLiteCache.cs" />
<Compile Include="SQLiteCache.UWP.cs" />
<EmbeddedResource Include="Properties\SQLiteCache.UWP.rd.xml" />
</ItemGroup>
<ItemGroup>