Clean/CleanAsync in cache implementations

This commit is contained in:
ClemensFischer 2024-02-10 20:47:30 +01:00
parent 7ab95c3bed
commit cdb7d4af15
3 changed files with 76 additions and 49 deletions

View file

@ -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<byte[]> 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))

View file

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

View file

@ -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;