2025-02-27 18:46:32 +01:00
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2024-02-03 20:53:32 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-02-25 18:19:57 +01:00
|
|
|
|
/// IDistributedCache implementation based on System.Data.SQLite, https://system.data.sqlite.org/.
|
2024-02-03 20:53:32 +01:00
|
|
|
|
/// </summary>
|
2024-08-20 17:59:39 +02:00
|
|
|
|
public sealed class SQLiteCache : IDistributedCache, IDisposable
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
|
|
|
|
|
private readonly SQLiteConnection connection;
|
2025-02-24 20:19:29 +01:00
|
|
|
|
private readonly Timer timer;
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
|
|
|
|
|
public SQLiteCache(string path)
|
2025-02-19 19:34:08 +01:00
|
|
|
|
: this(path, TimeSpan.FromHours(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-20 20:29:49 +01:00
|
|
|
|
public SQLiteCache(string path, TimeSpan expirationScanFrequency)
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
throw new ArgumentException($"The {nameof(path)} argument must not be null or empty.", nameof(path));
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Path.GetExtension(path)))
|
|
|
|
|
|
{
|
|
|
|
|
|
path = Path.Combine(path, "TileCache.sqlite");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-24 22:30:57 +01:00
|
|
|
|
connection = new SQLiteConnection("Data Source=" + path);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
2025-02-23 20:49:50 +01:00
|
|
|
|
using (var command = new SQLiteCommand("pragma journal_mode=wal", connection))
|
2025-02-23 20:42:34 +01:00
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
using (var command = new SQLiteCommand("create table if not exists items (key text primary key, expiration integer, buffer blob)", connection))
|
|
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}: Opened database {path}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-02-20 20:29:49 +01:00
|
|
|
|
if (expirationScanFrequency > TimeSpan.Zero)
|
2025-02-19 19:34:08 +01:00
|
|
|
|
{
|
2025-02-24 20:19:29 +01:00
|
|
|
|
timer = new Timer(_ => DeleteExpiredItems(), null, TimeSpan.Zero, expirationScanFrequency);
|
2025-02-19 19:34:08 +01:00
|
|
|
|
}
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2025-02-24 20:19:29 +01:00
|
|
|
|
timer?.Dispose();
|
2024-02-03 20:53:32 +01:00
|
|
|
|
connection.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] Get(string key)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
CheckArgument(key);
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
byte[] value = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = GetItemCommand(key))
|
|
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
value = (byte[])command.ExecuteScalar();
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}.Get({key}): {ex.Message}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
CheckArgument(key);
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
byte[] value = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = GetItemCommand(key))
|
|
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
value = (byte[])await command.ExecuteScalarAsync();
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}.GetAsync({key}): {ex.Message}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
CheckArguments(key, value, options);
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = SetItemCommand(key, value, options))
|
|
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}.Set({key}): {ex.Message}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
CheckArguments(key, value, options);
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = SetItemCommand(key, value, options))
|
|
|
|
|
|
{
|
|
|
|
|
|
await command.ExecuteNonQueryAsync(token);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}.SetAsync({key}): {ex.Message}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Refresh(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task RefreshAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
return Task.CompletedTask;
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Remove(string key)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
CheckArgument(key);
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
using (var command = DeleteItemCommand(key))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}.Remove({key}): {ex.Message}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task RemoveAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
2024-02-05 16:33:19 +01:00
|
|
|
|
CheckArgument(key);
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
using (var command = DeleteItemCommand(key))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
|
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}.RemoveAsync({key}): {ex.Message}");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-20 20:29:49 +01:00
|
|
|
|
public void DeleteExpiredItems()
|
2024-02-10 20:47:30 +01:00
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
using (var command = DeleteExpiredItemCommand())
|
2024-02-10 20:47:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
var deleted = (long)command.ExecuteScalar();
|
|
|
|
|
|
if (deleted > 0)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(SQLiteCache)}: Deleted {deleted} expired items");
|
2024-02-10 20:47:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
private SQLiteCommand GetItemCommand(string key)
|
|
|
|
|
|
{
|
2025-02-25 20:57:13 +01:00
|
|
|
|
var command = new SQLiteCommand("select buffer from items where key = @key and expiration > @exp", connection);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
command.Parameters.AddWithValue("@key", key);
|
2025-02-25 20:57:13 +01:00
|
|
|
|
command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
return command;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private SQLiteCommand SetItemCommand(string key, byte[] buffer, DistributedCacheEntryOptions options)
|
|
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
var expiration = options.AbsoluteExpiration ??
|
|
|
|
|
|
DateTimeOffset.UtcNow.Add(options.AbsoluteExpirationRelativeToNow ?? options.SlidingExpiration ?? TimeSpan.FromDays(1));
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
|
|
|
|
|
var command = new SQLiteCommand("insert or replace into items (key, expiration, buffer) values (@key, @exp, @buf)", connection);
|
|
|
|
|
|
command.Parameters.AddWithValue("@key", key);
|
2025-02-24 13:57:05 +01:00
|
|
|
|
command.Parameters.AddWithValue("@exp", expiration.UtcTicks);
|
2024-02-05 16:33:19 +01:00
|
|
|
|
command.Parameters.AddWithValue("@buf", buffer);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
return command;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-24 00:24:11 +01:00
|
|
|
|
private SQLiteCommand DeleteItemCommand(string key)
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-24 00:24:11 +01:00
|
|
|
|
var command = new SQLiteCommand("delete from items where key = @key", connection);
|
|
|
|
|
|
command.Parameters.AddWithValue("@key", key);
|
|
|
|
|
|
return command;
|
2024-02-05 16:33:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-24 00:24:11 +01:00
|
|
|
|
private SQLiteCommand DeleteExpiredItemCommand()
|
2024-02-05 16:33:19 +01:00
|
|
|
|
{
|
2025-02-25 20:57:13 +01:00
|
|
|
|
var command = new SQLiteCommand("delete from items where expiration <= @exp; select changes()", connection);
|
|
|
|
|
|
command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks);
|
2025-02-24 00:24:11 +01:00
|
|
|
|
return command;
|
2024-02-05 16:33:19 +01:00
|
|
|
|
}
|
2025-02-24 14:37:31 +01:00
|
|
|
|
|
|
|
|
|
|
private static void CheckArgument(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException($"The {nameof(key)} argument must not be null or empty.", nameof(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void CheckArguments(string key, byte[] value, DistributedCacheEntryOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
CheckArgument(key);
|
|
|
|
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
|
{
|
2025-02-27 18:46:32 +01:00
|
|
|
|
throw new ArgumentNullException(nameof(value), $"The {nameof(value)} argument must not be null.");
|
2025-02-24 14:37:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (options == null)
|
|
|
|
|
|
{
|
2025-02-27 18:46:32 +01:00
|
|
|
|
throw new ArgumentNullException(nameof(options), $"The {nameof(options)} argument must not be null.");
|
2025-02-24 14:37:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|