Cache implementations don't throw exceptions

This commit is contained in:
ClemensFischer 2025-02-28 18:26:59 +01:00
parent de446d3516
commit c17fa5c485
3 changed files with 229 additions and 251 deletions

View file

@ -27,14 +27,9 @@ namespace MapControl.Caching
public FileDbCache(string path, TimeSpan expirationScanFrequency)
{
if (string.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(Path.GetExtension(path)))
{
throw new ArgumentException($"The {nameof(path)} argument must not be null or empty.", nameof(path));
}
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = Path.Combine(path, "TileCache.fdb");
path = Path.Combine(path ?? "", "TileCache.fdb");
}
try
@ -78,22 +73,23 @@ namespace MapControl.Caching
public byte[] Get(string key)
{
CheckArgument(key);
byte[] value = null;
try
if (!string.IsNullOrEmpty(key))
{
var record = fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false);
if (record != null && (DateTime)record[1] > DateTime.UtcNow)
try
{
value = (byte[])record[0];
var record = fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false);
if (record != null && (DateTime)record[1] > DateTime.UtcNow)
{
value = (byte[])record[0];
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(FileDbCache)}.Get({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(FileDbCache)}.Get({key}): {ex.Message}");
}
return value;
@ -106,33 +102,34 @@ namespace MapControl.Caching
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
CheckArguments(key, value, options);
var expiration = options.AbsoluteExpiration.HasValue
? options.AbsoluteExpiration.Value.UtcDateTime
: DateTime.UtcNow.Add(options.AbsoluteExpirationRelativeToNow ?? options.SlidingExpiration ?? TimeSpan.FromDays(1));
var fieldValues = new FieldValues(3)
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
{ valueField, value },
{ expiresField, expiration }
};
var expiration = options.AbsoluteExpiration.HasValue
? options.AbsoluteExpiration.Value.UtcDateTime
: DateTime.UtcNow.Add(options.AbsoluteExpirationRelativeToNow ?? options.SlidingExpiration ?? TimeSpan.FromDays(1));
try
{
if (fileDb.GetRecordByKey(key, new string[0], false) != null)
var fieldValues = new FieldValues(3)
{
fileDb.UpdateRecordByKey(key, fieldValues);
}
else
{ valueField, value },
{ expiresField, expiration }
};
try
{
fieldValues.Add(keyField, key);
fileDb.AddRecord(fieldValues);
if (fileDb.GetRecordByKey(key, new string[0], false) != null)
{
fileDb.UpdateRecordByKey(key, fieldValues);
}
else
{
fieldValues.Add(keyField, key);
fileDb.AddRecord(fieldValues);
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(FileDbCache)}.Set({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(FileDbCache)}.Set({key}): {ex.Message}");
}
}
@ -145,25 +142,25 @@ namespace MapControl.Caching
public void Refresh(string key)
{
throw new NotSupportedException();
}
public Task RefreshAsync(string key, CancellationToken token = default)
{
throw new NotSupportedException();
return Task.CompletedTask;
}
public void Remove(string key)
{
CheckArgument(key);
try
if (!string.IsNullOrEmpty(key))
{
fileDb.DeleteRecordByKey(key);
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(FileDbCache)}.Remove({key}): {ex.Message}");
try
{
fileDb.DeleteRecordByKey(key);
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(FileDbCache)}.Remove({key}): {ex.Message}");
}
}
}
@ -185,28 +182,5 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(FileDbCache)}: Deleted {deleted} expired items");
}
}
private static void CheckArgument(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException($"The {nameof(key)} argument must not be null or empty.", nameof(key));
}
}
private static void CheckArguments(string key, byte[] value, DistributedCacheEntryOptions options)
{
CheckArgument(key);
if (value == null)
{
throw new ArgumentNullException(nameof(value), $"The {nameof(value)} argument must not be null.");
}
if (options == null)
{
throw new ArgumentNullException(nameof(options), $"The {nameof(options)} argument must not be null.");
}
}
}
}

View file

@ -23,14 +23,9 @@ namespace MapControl.Caching
public SQLiteCache(string path, TimeSpan expirationScanFrequency)
{
if (string.IsNullOrEmpty(path))
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(Path.GetExtension(path)))
{
throw new ArgumentException($"The {nameof(path)} argument must not be null or empty.", nameof(path));
}
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = Path.Combine(path, "TileCache.sqlite");
path = Path.Combine(path ?? "", "TileCache.sqlite");
}
connection = new SQLiteConnection("Data Source=" + path);
@ -62,20 +57,21 @@ namespace MapControl.Caching
public byte[] Get(string key)
{
CheckArgument(key);
byte[] value = null;
try
if (!string.IsNullOrEmpty(key))
{
using (var command = GetItemCommand(key))
try
{
value = (byte[])command.ExecuteScalar();
using (var command = GetItemCommand(key))
{
value = (byte[])command.ExecuteScalar();
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Get({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Get({key}): {ex.Message}");
}
return value;
@ -83,20 +79,21 @@ namespace MapControl.Caching
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
{
CheckArgument(key);
byte[] value = null;
try
if (!string.IsNullOrEmpty(key))
{
using (var command = GetItemCommand(key))
try
{
value = (byte[])await command.ExecuteScalarAsync();
using (var command = GetItemCommand(key))
{
value = (byte[])await command.ExecuteScalarAsync();
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.GetAsync({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.GetAsync({key}): {ex.Message}");
}
return value;
@ -104,79 +101,82 @@ namespace MapControl.Caching
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
CheckArguments(key, value, options);
try
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
using (var command = SetItemCommand(key, value, options))
try
{
command.ExecuteNonQuery();
using (var command = SetItemCommand(key, value, options))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Set({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Set({key}): {ex.Message}");
}
}
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
CheckArguments(key, value, options);
try
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
using (var command = SetItemCommand(key, value, options))
try
{
await command.ExecuteNonQueryAsync(token);
using (var command = SetItemCommand(key, value, options))
{
await command.ExecuteNonQueryAsync(token);
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.SetAsync({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.SetAsync({key}): {ex.Message}");
}
}
public void Refresh(string key)
{
throw new NotSupportedException();
}
public Task RefreshAsync(string key, CancellationToken token = default)
{
throw new NotSupportedException();
return Task.CompletedTask;
}
public void Remove(string key)
{
CheckArgument(key);
try
if (!string.IsNullOrEmpty(key))
{
using (var command = DeleteItemCommand(key))
try
{
command.ExecuteNonQuery();
using (var command = DeleteItemCommand(key))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Remove({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Remove({key}): {ex.Message}");
}
}
public async Task RemoveAsync(string key, CancellationToken token = default)
{
CheckArgument(key);
try
if (!string.IsNullOrEmpty(key))
{
using (var command = DeleteItemCommand(key))
try
{
await command.ExecuteNonQueryAsync();
using (var command = DeleteItemCommand(key))
{
await command.ExecuteNonQueryAsync();
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.RemoveAsync({key}): {ex.Message}");
}
}
catch (Exception ex)
{
Debug.WriteLine($"{nameof(SQLiteCache)}.RemoveAsync({key}): {ex.Message}");
}
}
@ -225,28 +225,5 @@ namespace MapControl.Caching
command.Parameters.AddWithValue("@exp", DateTimeOffset.UtcNow.Ticks);
return command;
}
private static void CheckArgument(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException($"The {nameof(key)} argument must not be null or empty.", nameof(key));
}
}
private static void CheckArguments(string key, byte[] value, DistributedCacheEntryOptions options)
{
CheckArgument(key);
if (value == null)
{
throw new ArgumentNullException(nameof(value), $"The {nameof(value)} argument must not be null.");
}
if (options == null)
{
throw new ArgumentNullException(nameof(options), $"The {nameof(options)} argument must not be null.");
}
}
}
}