Updated cache implementations

This commit is contained in:
ClemensFischer 2025-02-20 20:29:49 +01:00
parent e7c2213f71
commit ed87d87f75
4 changed files with 90 additions and 99 deletions

View file

@ -23,14 +23,14 @@ namespace MapControl.Caching
private const string expiresField = "Expires";
private readonly FileDb fileDb = new FileDb { AutoFlush = true };
private readonly Timer cleanTimer;
private readonly Timer expirationScanTimer;
public FileDbCache(string path)
: this(path, TimeSpan.FromHours(1))
{
}
public FileDbCache(string path, TimeSpan autoCleanInterval)
public FileDbCache(string path, TimeSpan expirationScanFrequency)
{
if (string.IsNullOrEmpty(path))
{
@ -45,6 +45,7 @@ namespace MapControl.Caching
try
{
fileDb.Open(path);
Debug.WriteLine($"{nameof(FileDbCache)}: Opened database {path}");
}
catch
@ -68,15 +69,15 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(FileDbCache)}: Created database {path}");
}
if (autoCleanInterval > TimeSpan.Zero)
if (expirationScanFrequency > TimeSpan.Zero)
{
cleanTimer = new Timer(_ => Clean(), null, TimeSpan.Zero, autoCleanInterval);
expirationScanTimer = new Timer(_ => DeleteExpiredItems(), null, TimeSpan.Zero, expirationScanFrequency);
}
}
public void Dispose()
{
cleanTimer?.Dispose();
expirationScanTimer?.Dispose();
fileDb.Dispose();
}
@ -92,7 +93,7 @@ namespace MapControl.Caching
if (record != null)
{
if ((DateTime)record[1] > DateTime.UtcNow)
if ((DateTime)record[1] > DateTime.Now)
{
value = (byte[])record[0];
}
@ -114,24 +115,9 @@ namespace MapControl.Caching
{
CheckArguments(key, value, options);
DateTime expiration;
if (options.AbsoluteExpiration.HasValue)
{
expiration = options.AbsoluteExpiration.Value.DateTime;
}
else if (options.AbsoluteExpirationRelativeToNow.HasValue)
{
expiration = DateTime.UtcNow.Add(options.AbsoluteExpirationRelativeToNow.Value);
}
else if (options.SlidingExpiration.HasValue)
{
expiration = DateTime.UtcNow.Add(options.SlidingExpiration.Value);
}
else
{
expiration = DateTime.UtcNow.Add(TimeSpan.FromDays(1));
}
var expiration = options.AbsoluteExpiration.HasValue
? options.AbsoluteExpiration.Value.LocalDateTime
: DateTime.Now.Add(options.AbsoluteExpirationRelativeToNow ?? (options.SlidingExpiration ?? TimeSpan.FromDays(1)));
var fieldValues = new FieldValues(3)
{
@ -175,9 +161,9 @@ namespace MapControl.Caching
}
}
public void Clean()
public void DeleteExpiredItems()
{
var deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.UtcNow, ComparisonOperatorEnum.LessThanOrEqual));
var deleted = fileDb.DeleteRecords(new FilterExpression(expiresField, DateTime.Now, ComparisonOperatorEnum.LessThanOrEqual));
if (deleted > 0)
{
@ -212,13 +198,6 @@ 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

@ -19,14 +19,14 @@ namespace MapControl.Caching
public sealed class SQLiteCache : IDistributedCache, IDisposable
{
private readonly SQLiteConnection connection;
private readonly Timer cleanTimer;
private readonly Timer expirationScanTimer;
public SQLiteCache(string path)
: this(path, TimeSpan.FromHours(1))
{
}
public SQLiteCache(string path, TimeSpan autoCleanInterval)
public SQLiteCache(string path, TimeSpan expirationScanFrequency)
{
if (string.IsNullOrEmpty(path))
{
@ -48,15 +48,15 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(SQLiteCache)}: Opened database {path}");
if (autoCleanInterval > TimeSpan.Zero)
if (expirationScanFrequency > TimeSpan.Zero)
{
cleanTimer = new Timer(_ => Clean(), null, TimeSpan.Zero, autoCleanInterval);
expirationScanTimer = new Timer(_ => DeleteExpiredItems(), null, TimeSpan.Zero, expirationScanFrequency);
}
}
public void Dispose()
{
cleanTimer?.Dispose();
expirationScanTimer?.Dispose();
connection.Dispose();
}
@ -189,11 +189,11 @@ namespace MapControl.Caching
}
}
public void Clean()
public void DeleteExpiredItems()
{
using (var command = new SQLiteCommand("delete from items where expiration < @exp", connection))
{
command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks);
command.Parameters.AddWithValue("@exp", DateTimeOffset.Now.Ticks);
command.ExecuteNonQuery();
}
#if DEBUG
@ -208,11 +208,11 @@ namespace MapControl.Caching
#endif
}
public async Task CleanAsync()
public async Task DeleteExpiredItemsAsync()
{
using (var command = new SQLiteCommand("delete from items where expiration < @exp", connection))
{
command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks);
command.Parameters.AddWithValue("@exp", DateTimeOffset.Now.Ticks);
await command.ExecuteNonQueryAsync();
}
#if DEBUG
@ -243,24 +243,9 @@ namespace MapControl.Caching
private SQLiteCommand SetItemCommand(string key, byte[] buffer, DistributedCacheEntryOptions options)
{
DateTimeOffset expiration;
if (options.AbsoluteExpiration.HasValue)
{
expiration = options.AbsoluteExpiration.Value;
}
else if (options.AbsoluteExpirationRelativeToNow.HasValue)
{
expiration = DateTimeOffset.UtcNow.Add(options.AbsoluteExpirationRelativeToNow.Value);
}
else if (options.SlidingExpiration.HasValue)
{
expiration = DateTimeOffset.UtcNow.Add(options.SlidingExpiration.Value);
}
else
{
expiration = DateTimeOffset.UtcNow.Add(TimeSpan.FromDays(1));
}
var expiration = options.AbsoluteExpiration.HasValue
? options.AbsoluteExpiration.Value
: DateTimeOffset.Now.Add(options.AbsoluteExpirationRelativeToNow ?? (options.SlidingExpiration ?? TimeSpan.FromDays(1)));
var command = new SQLiteCommand("insert or replace into items (key, expiration, buffer) values (@key, @exp, @buf)", connection);
command.Parameters.AddWithValue("@key", key);
@ -273,7 +258,7 @@ namespace MapControl.Caching
{
var expiration = new DateTimeOffset((long)reader["expiration"], TimeSpan.Zero);
if (expiration <= DateTimeOffset.UtcNow)
if (expiration <= DateTimeOffset.Now)
{
return false;
}