From d31f522c520d2132030e350a5e9de32dd13bdbb4 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Wed, 19 Feb 2025 19:34:08 +0100 Subject: [PATCH] Cyclic clean --- Caches/FileDbCache/FileDbCache.cs | 14 ++++++++++++-- Caches/SQLiteCache/SQLiteCache.cs | 12 +++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Caches/FileDbCache/FileDbCache.cs b/Caches/FileDbCache/FileDbCache.cs index fd249633..b41da10a 100644 --- a/Caches/FileDbCache/FileDbCache.cs +++ b/Caches/FileDbCache/FileDbCache.cs @@ -23,8 +23,14 @@ namespace MapControl.Caching private const string expiresField = "Expires"; private readonly FileDb fileDb = new FileDb { AutoFlush = true }; + private readonly Timer cleanTimer; public FileDbCache(string path) + : this(path, TimeSpan.FromHours(1)) + { + } + + public FileDbCache(string path, TimeSpan autoCleanInterval) { if (string.IsNullOrEmpty(path)) { @@ -40,8 +46,6 @@ namespace MapControl.Caching { fileDb.Open(path); Debug.WriteLine($"{nameof(FileDbCache)}: Opened database {path}"); - - Clean(); } catch { @@ -63,10 +67,16 @@ namespace MapControl.Caching Debug.WriteLine($"{nameof(FileDbCache)}: Created database {path}"); } + + if (autoCleanInterval > TimeSpan.Zero) + { + cleanTimer = new Timer(_ => Clean(), null, TimeSpan.Zero, autoCleanInterval); + } } public void Dispose() { + cleanTimer?.Dispose(); fileDb.Dispose(); } diff --git a/Caches/SQLiteCache/SQLiteCache.cs b/Caches/SQLiteCache/SQLiteCache.cs index f8ac0209..b5d47d59 100644 --- a/Caches/SQLiteCache/SQLiteCache.cs +++ b/Caches/SQLiteCache/SQLiteCache.cs @@ -19,8 +19,14 @@ namespace MapControl.Caching public sealed class SQLiteCache : IDistributedCache, IDisposable { private readonly SQLiteConnection connection; + private readonly Timer cleanTimer; public SQLiteCache(string path) + : this(path, TimeSpan.FromHours(1)) + { + } + + public SQLiteCache(string path, TimeSpan autoCleanInterval) { if (string.IsNullOrEmpty(path)) { @@ -42,11 +48,15 @@ namespace MapControl.Caching Debug.WriteLine($"{nameof(SQLiteCache)}: Opened database {path}"); - Clean(); + if (autoCleanInterval > TimeSpan.Zero) + { + cleanTimer = new Timer(_ => Clean(), null, TimeSpan.Zero, autoCleanInterval); + } } public void Dispose() { + cleanTimer?.Dispose(); connection.Dispose(); }