2025-02-27 18:46:32 +01:00
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2025-03-30 16:35:10 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2024-02-03 20:53:32 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
public class SQLiteCacheOptions : IOptions<SQLiteCacheOptions>
|
|
|
|
|
|
{
|
|
|
|
|
|
public SQLiteCacheOptions Value => this;
|
|
|
|
|
|
|
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public TimeSpan ExpirationScanFrequency { get; set; } = TimeSpan.FromHours(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
/// <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;
|
2025-03-30 16:35:10 +02:00
|
|
|
|
private readonly ILogger logger;
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-03-30 16:35:10 +02:00
|
|
|
|
public SQLiteCache(string path, ILoggerFactory loggerFactory = null)
|
|
|
|
|
|
: this(new SQLiteCacheOptions { Path = path }, loggerFactory)
|
2025-02-19 19:34:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-30 16:35:10 +02:00
|
|
|
|
public SQLiteCache(IOptions<SQLiteCacheOptions> optionsAccessor, ILoggerFactory loggerFactory = null)
|
|
|
|
|
|
: this(optionsAccessor.Value, loggerFactory)
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public SQLiteCache(SQLiteCacheOptions options, ILoggerFactory loggerFactory = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = options.Path;
|
|
|
|
|
|
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(Path.GetExtension(path)))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
path = Path.Combine(path ?? "", "TileCache.sqlite");
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-31 20:55:57 +02:00
|
|
|
|
logger = loggerFactory?.CreateLogger<SQLiteCache>();
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogInformation("Opened database {path}", path);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-03-30 16:35:10 +02:00
|
|
|
|
if (options.ExpirationScanFrequency > TimeSpan.Zero)
|
2025-02-19 19:34:08 +01:00
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
timer = new Timer(_ => DeleteExpiredItems(), null, TimeSpan.Zero, options.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)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] value = null;
|
|
|
|
|
|
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
using (var command = GetItemCommand(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
value = (byte[])command.ExecuteScalar();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogError(ex, "Get({key})", key);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] value = null;
|
|
|
|
|
|
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
using (var command = GetItemCommand(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
value = (byte[])await command.ExecuteScalarAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogError(ex, "GetAsync({key})", key);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
|
|
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(key) && value != null && options != null)
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
using (var command = SetItemCommand(key, value, options))
|
|
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogError(ex, "Set({key})", key);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
|
|
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(key) && value != null && options != null)
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
using (var command = SetItemCommand(key, value, options))
|
|
|
|
|
|
{
|
|
|
|
|
|
await command.ExecuteNonQueryAsync(token);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogError(ex, "SetAsync({key})", key);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Refresh(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task RefreshAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
return Task.CompletedTask;
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Remove(string key)
|
|
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
using (var command = DeleteItemCommand(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogError(ex, "Remove({key})", key);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task RemoveAsync(string key, CancellationToken token = default)
|
|
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-02-28 18:26:59 +01:00
|
|
|
|
using (var command = DeleteItemCommand(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogError(ex, "RemoveAsync({key})", key);
|
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-03-30 16:35:10 +02:00
|
|
|
|
long deletedItemsCount;
|
2025-03-01 13:55:02 +01:00
|
|
|
|
|
|
|
|
|
|
using (var command = DeleteExpiredItemsCommand())
|
2024-02-10 20:47:30 +01:00
|
|
|
|
{
|
2025-03-30 16:35:10 +02:00
|
|
|
|
deletedItemsCount = (long)command.ExecuteScalar();
|
2025-03-01 13:55:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-30 16:35:10 +02:00
|
|
|
|
if (deletedItemsCount > 0)
|
2025-03-01 13:55:02 +01:00
|
|
|
|
{
|
|
|
|
|
|
using (var command = new SQLiteCommand("vacuum", connection))
|
2024-02-10 20:47:30 +01:00
|
|
|
|
{
|
2025-03-01 13:55:02 +01:00
|
|
|
|
command.ExecuteNonQuery();
|
2024-02-10 20:47:30 +01:00
|
|
|
|
}
|
2025-03-01 13:55:02 +01:00
|
|
|
|
|
2025-03-30 16:35:10 +02:00
|
|
|
|
logger?.LogInformation("Deleted {count} expired items", deletedItemsCount);
|
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-03-01 13:55:02 +01:00
|
|
|
|
private SQLiteCommand DeleteExpiredItemsCommand()
|
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
|
|
|
|
}
|
2024-02-03 20:53:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|