Cyclic clean

This commit is contained in:
ClemensFischer 2025-02-19 19:34:08 +01:00
parent df4b330d08
commit d31f522c52
2 changed files with 23 additions and 3 deletions

View file

@ -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();
}

View file

@ -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();
}