From cdb7d4af15307ea12235e696ec1c0e27015ac7af Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Sat, 10 Feb 2024 20:47:30 +0100 Subject: [PATCH] Clean/CleanAsync in cache implementations --- Caches/FileDbCache/FileDbCache.cs | 28 ++++++++------ Caches/SQLiteCache/SQLiteCache.cs | 57 +++++++++++++++++++---------- MapControl/Shared/ImageFileCache.cs | 40 ++++++++++---------- 3 files changed, 76 insertions(+), 49 deletions(-) diff --git a/Caches/FileDbCache/FileDbCache.cs b/Caches/FileDbCache/FileDbCache.cs index 8b292d51..ba6aec9e 100644 --- a/Caches/FileDbCache/FileDbCache.cs +++ b/Caches/FileDbCache/FileDbCache.cs @@ -70,17 +70,6 @@ namespace MapControl.Caching fileDb.Dispose(); } - public void Clean() - { - var deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThanOrEqual)); - - if (deleted > 0) - { - Debug.WriteLine($"FileDbCache: Deleted {deleted} expired items"); - fileDb.Clean(); - } - } - public byte[] Get(string key) { CheckArgument(key); @@ -177,6 +166,17 @@ namespace MapControl.Caching } } + public void Clean() + { + var deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThanOrEqual)); + + if (deleted > 0) + { + Debug.WriteLine($"FileDbCache: Deleted {deleted} expired items"); + fileDb.Clean(); + } + } + public Task GetAsync(string key, CancellationToken token = default) { return Task.FromResult(Get(key)); @@ -200,6 +200,12 @@ namespace MapControl.Caching return Task.CompletedTask; } + public Task CleanAsync() + { + Clean(); + return Task.CompletedTask; + } + private static void CheckArgument(string key) { if (string.IsNullOrEmpty(key)) diff --git a/Caches/SQLiteCache/SQLiteCache.cs b/Caches/SQLiteCache/SQLiteCache.cs index 3e5f2a0f..b29d6bbf 100644 --- a/Caches/SQLiteCache/SQLiteCache.cs +++ b/Caches/SQLiteCache/SQLiteCache.cs @@ -50,25 +50,6 @@ namespace MapControl.Caching connection.Dispose(); } - public void Clean() - { - using (var command = new SQLiteCommand("delete from items where expiration < @exp", connection)) - { - command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks); - command.ExecuteNonQuery(); - } -#if DEBUG - using (var command = new SQLiteCommand("select changes()", connection)) - { - var deleted = (long)command.ExecuteScalar(); - if (deleted > 0) - { - Debug.WriteLine($"SQLiteCache: Deleted {deleted} expired items"); - } - } -#endif - } - public byte[] Get(string key) { CheckArgument(key); @@ -201,6 +182,44 @@ namespace MapControl.Caching } } + public void Clean() + { + using (var command = new SQLiteCommand("delete from items where expiration < @exp", connection)) + { + command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks); + command.ExecuteNonQuery(); + } +#if DEBUG + using (var command = new SQLiteCommand("select changes()", connection)) + { + var deleted = (long)command.ExecuteScalar(); + if (deleted > 0) + { + Debug.WriteLine($"SQLiteCache: Deleted {deleted} expired items"); + } + } +#endif + } + + public async Task CleanAsync() + { + using (var command = new SQLiteCommand("delete from items where expiration < @exp", connection)) + { + command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks); + await command.ExecuteNonQueryAsync(); + } +#if DEBUG + using (var command = new SQLiteCommand("select changes()", connection)) + { + var deleted = (long)await command.ExecuteScalarAsync(); + if (deleted > 0) + { + Debug.WriteLine($"SQLiteCache: Deleted {deleted} expired items"); + } + } +#endif + } + private SQLiteCommand GetItemCommand(string key) { var command = new SQLiteCommand("select expiration, buffer from items where key = @key", connection); diff --git a/MapControl/Shared/ImageFileCache.cs b/MapControl/Shared/ImageFileCache.cs index 33ff71c3..7857088b 100644 --- a/MapControl/Shared/ImageFileCache.cs +++ b/MapControl/Shared/ImageFileCache.cs @@ -38,11 +38,8 @@ namespace MapControl.Caching rootDirectory = directory; Debug.WriteLine($"Created ImageFileCache in {rootDirectory}"); - } - public Task CleanAsync() - { - return Task.Factory.StartNew(CleanRootDirectory, TaskCreationOptions.LongRunning); + ThreadPool.QueueUserWorkItem(o => Clean()); } public byte[] Get(string key) @@ -253,21 +250,7 @@ namespace MapControl.Caching } } - private string GetPath(string key) - { - try - { - return Path.Combine(rootDirectory, Path.Combine(key.Split('/'))); - } - catch (Exception ex) - { - Debug.WriteLine($"ImageFileCache: Invalid key {rootDirectory}/{key}: {ex.Message}"); - } - - return null; - } - - private void CleanRootDirectory() + public void Clean() { try { @@ -287,6 +270,25 @@ namespace MapControl.Caching } } + public Task CleanAsync() + { + return Task.Factory.StartNew(Clean, TaskCreationOptions.LongRunning); + } + + private string GetPath(string key) + { + try + { + return Path.Combine(rootDirectory, Path.Combine(key.Split('/'))); + } + catch (Exception ex) + { + Debug.WriteLine($"ImageFileCache: Invalid key {rootDirectory}/{key}: {ex.Message}"); + } + + return null; + } + private static int CleanDirectory(DirectoryInfo directory) { var deletedFileCount = 0;