mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-20 22:05:07 +00:00
Clean/CleanAsync in cache implementations
This commit is contained in:
parent
7ab95c3bed
commit
cdb7d4af15
3 changed files with 76 additions and 49 deletions
|
|
@ -70,17 +70,6 @@ namespace MapControl.Caching
|
||||||
fileDb.Dispose();
|
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)
|
public byte[] Get(string key)
|
||||||
{
|
{
|
||||||
CheckArgument(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)
|
public Task<byte[]> GetAsync(string key, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
return Task.FromResult(Get(key));
|
return Task.FromResult(Get(key));
|
||||||
|
|
@ -200,6 +200,12 @@ namespace MapControl.Caching
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task CleanAsync()
|
||||||
|
{
|
||||||
|
Clean();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
private static void CheckArgument(string key)
|
private static void CheckArgument(string key)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(key))
|
if (string.IsNullOrEmpty(key))
|
||||||
|
|
|
||||||
|
|
@ -50,25 +50,6 @@ namespace MapControl.Caching
|
||||||
connection.Dispose();
|
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)
|
public byte[] Get(string key)
|
||||||
{
|
{
|
||||||
CheckArgument(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)
|
private SQLiteCommand GetItemCommand(string key)
|
||||||
{
|
{
|
||||||
var command = new SQLiteCommand("select expiration, buffer from items where key = @key", connection);
|
var command = new SQLiteCommand("select expiration, buffer from items where key = @key", connection);
|
||||||
|
|
|
||||||
|
|
@ -38,11 +38,8 @@ namespace MapControl.Caching
|
||||||
rootDirectory = directory;
|
rootDirectory = directory;
|
||||||
|
|
||||||
Debug.WriteLine($"Created ImageFileCache in {rootDirectory}");
|
Debug.WriteLine($"Created ImageFileCache in {rootDirectory}");
|
||||||
}
|
|
||||||
|
|
||||||
public Task CleanAsync()
|
ThreadPool.QueueUserWorkItem(o => Clean());
|
||||||
{
|
|
||||||
return Task.Factory.StartNew(CleanRootDirectory, TaskCreationOptions.LongRunning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] Get(string key)
|
public byte[] Get(string key)
|
||||||
|
|
@ -253,21 +250,7 @@ namespace MapControl.Caching
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetPath(string key)
|
public void Clean()
|
||||||
{
|
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
try
|
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)
|
private static int CleanDirectory(DirectoryInfo directory)
|
||||||
{
|
{
|
||||||
var deletedFileCount = 0;
|
var deletedFileCount = 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue