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,10 +73,10 @@ namespace MapControl.Caching
public byte[] Get(string key)
{
CheckArgument(key);
byte[] value = null;
if (!string.IsNullOrEmpty(key))
{
try
{
var record = fileDb.GetRecordByKey(key, new string[] { valueField, expiresField }, false);
@ -95,6 +90,7 @@ namespace MapControl.Caching
{
Debug.WriteLine($"{nameof(FileDbCache)}.Get({key}): {ex.Message}");
}
}
return value;
}
@ -106,8 +102,8 @@ namespace MapControl.Caching
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
CheckArguments(key, value, options);
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
var expiration = options.AbsoluteExpiration.HasValue
? options.AbsoluteExpiration.Value.UtcDateTime
: DateTime.UtcNow.Add(options.AbsoluteExpirationRelativeToNow ?? options.SlidingExpiration ?? TimeSpan.FromDays(1));
@ -135,6 +131,7 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(FileDbCache)}.Set({key}): {ex.Message}");
}
}
}
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
@ -145,18 +142,17 @@ 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);
if (!string.IsNullOrEmpty(key))
{
try
{
fileDb.DeleteRecordByKey(key);
@ -166,6 +162,7 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(FileDbCache)}.Remove({key}): {ex.Message}");
}
}
}
public Task RemoveAsync(string key, CancellationToken token = default)
{
@ -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,10 +57,10 @@ namespace MapControl.Caching
public byte[] Get(string key)
{
CheckArgument(key);
byte[] value = null;
if (!string.IsNullOrEmpty(key))
{
try
{
using (var command = GetItemCommand(key))
@ -77,16 +72,17 @@ namespace MapControl.Caching
{
Debug.WriteLine($"{nameof(SQLiteCache)}.Get({key}): {ex.Message}");
}
}
return value;
}
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
{
CheckArgument(key);
byte[] value = null;
if (!string.IsNullOrEmpty(key))
{
try
{
using (var command = GetItemCommand(key))
@ -98,14 +94,15 @@ namespace MapControl.Caching
{
Debug.WriteLine($"{nameof(SQLiteCache)}.GetAsync({key}): {ex.Message}");
}
}
return value;
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
CheckArguments(key, value, options);
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
try
{
using (var command = SetItemCommand(key, value, options))
@ -118,11 +115,12 @@ namespace MapControl.Caching
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);
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
try
{
using (var command = SetItemCommand(key, value, options))
@ -135,21 +133,21 @@ namespace MapControl.Caching
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);
if (!string.IsNullOrEmpty(key))
{
try
{
using (var command = DeleteItemCommand(key))
@ -162,11 +160,12 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(SQLiteCache)}.Remove({key}): {ex.Message}");
}
}
}
public async Task RemoveAsync(string key, CancellationToken token = default)
{
CheckArgument(key);
if (!string.IsNullOrEmpty(key))
{
try
{
using (var command = DeleteItemCommand(key))
@ -179,6 +178,7 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(SQLiteCache)}.RemoveAsync({key}): {ex.Message}");
}
}
}
public void DeleteExpiredItems()
{
@ -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.");
}
}
}
}

View file

@ -29,7 +29,7 @@ namespace MapControl.Caching
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException($"The {nameof(path)} argument must not be null or empty.", nameof(path));
path = "TileCache";
}
rootDirectory = new DirectoryInfo(path);
@ -56,9 +56,13 @@ namespace MapControl.Caching
public byte[] Get(string key)
{
var buffer = memoryCache.Get(key);
byte[] value = null;
if (buffer == null)
if (!string.IsNullOrEmpty(key))
{
value = memoryCache.Get(key);
if (value == null)
{
var file = GetFile(key);
@ -66,11 +70,11 @@ namespace MapControl.Caching
{
if (file != null && file.Exists && file.CreationTime > DateTime.Now)
{
buffer = ReadAllBytes(file);
value = ReadAllBytes(file);
var options = new DistributedCacheEntryOptions { AbsoluteExpiration = file.CreationTime };
memoryCache.Set(key, buffer, options);
memoryCache.Set(key, value, options);
}
}
catch (Exception ex)
@ -78,15 +82,20 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed reading {file.FullName}: {ex.Message}");
}
}
}
return buffer;
return value;
}
public async Task<byte[]> GetAsync(string key, CancellationToken token = default)
{
var buffer = await memoryCache.GetAsync(key, token).ConfigureAwait(false);
byte[] value = null;
if (buffer == null)
if (!string.IsNullOrEmpty(key))
{
value = await memoryCache.GetAsync(key, token).ConfigureAwait(false);
if (value == null)
{
var file = GetFile(key);
@ -94,38 +103,40 @@ namespace MapControl.Caching
{
if (file != null && file.Exists && file.CreationTime > DateTime.Now && !token.IsCancellationRequested)
{
buffer = await ReadAllBytes(file, token).ConfigureAwait(false);
value = await ReadAllBytes(file, token).ConfigureAwait(false);
var options = new DistributedCacheEntryOptions { AbsoluteExpiration = file.CreationTime };
await memoryCache.SetAsync(key, buffer, options, token).ConfigureAwait(false);
await memoryCache.SetAsync(key, value, options, token).ConfigureAwait(false);
}
}
catch (Exception ex)
{
buffer = null;
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed reading {file.FullName}: {ex.Message}");
}
}
return buffer;
}
public void Set(string key, byte[] buffer, DistributedCacheEntryOptions options)
return value;
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
memoryCache.Set(key, buffer, options);
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
memoryCache.Set(key, value, options);
var file = GetFile(key);
try
{
if (file != null && buffer?.Length > 0)
if (file != null && value?.Length > 0)
{
file.Directory.Create();
using (var stream = file.Create())
{
stream.Write(buffer, 0, buffer.Length);
stream.Write(value, 0, value.Length);
}
SetExpiration(file, options);
@ -136,22 +147,25 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed writing {file.FullName}: {ex.Message}");
}
}
}
public async Task SetAsync(string key, byte[] buffer, DistributedCacheEntryOptions options, CancellationToken token = default)
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
await memoryCache.SetAsync(key, buffer, options, token).ConfigureAwait(false);
if (!string.IsNullOrEmpty(key) && value != null && options != null)
{
await memoryCache.SetAsync(key, value, options, token).ConfigureAwait(false);
var file = GetFile(key);
try
{
if (file != null && buffer?.Length > 0 && !token.IsCancellationRequested)
if (file != null && value?.Length > 0 && !token.IsCancellationRequested)
{
file.Directory.Create();
using (var stream = file.Create())
{
await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false);
await stream.WriteAsync(value, 0, value.Length, token).ConfigureAwait(false);
}
SetExpiration(file, options);
@ -162,18 +176,27 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed writing {file.FullName}: {ex.Message}");
}
}
}
public void Refresh(string key)
{
if (!string.IsNullOrEmpty(key))
{
memoryCache.Refresh(key);
}
}
public Task RefreshAsync(string key, CancellationToken token = default)
public async Task RefreshAsync(string key, CancellationToken token = default)
{
return memoryCache.RefreshAsync(key, token);
if (!string.IsNullOrEmpty(key))
{
await memoryCache.RefreshAsync(key, token);
}
}
public void Remove(string key)
{
if (!string.IsNullOrEmpty(key))
{
memoryCache.Remove(key);
@ -191,8 +214,11 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed deleting {file.FullName}: {ex.Message}");
}
}
}
public async Task RemoveAsync(string key, CancellationToken token = default)
{
if (!string.IsNullOrEmpty(key))
{
await memoryCache.RemoveAsync(key, token);
@ -210,6 +236,7 @@ namespace MapControl.Caching
Debug.WriteLine($"{nameof(ImageFileCache)}: Failed deleting {file.FullName}: {ex.Message}");
}
}
}
public void DeleteExpiredItems()
{