Update ImageFileCache.cs

This commit is contained in:
ClemensFischer 2024-02-05 14:50:30 +01:00
parent 39671f66ae
commit 08adde21b8

View file

@ -135,13 +135,11 @@ namespace MapControl.Caching
public void Set(string key, byte[] buffer, DistributedCacheEntryOptions options)
{
memoryCache.Set(key, buffer, options);
var path = GetPath(key);
if (path != null && buffer != null)
{
memoryCache.Set(key, buffer, options); // buffer may be empty
if (buffer.Length > 0)
if (path != null && buffer?.Length > 0)
{
try
{
@ -170,19 +168,16 @@ namespace MapControl.Caching
}
}
}
}
public async Task SetAsync(string key, byte[] buffer, DistributedCacheEntryOptions options, CancellationToken token = default)
{
await memoryCache.SetAsync(key, buffer, options, token).ConfigureAwait(false);
var path = GetPath(key);
if (path != null && buffer != null)
if (path != null && buffer?.Length > 0 && !token.IsCancellationRequested)
{
try
{
await memoryCache.SetAsync(key, buffer, options, token).ConfigureAwait(false); // buffer may be empty
if (buffer.Length > 0 && !token.IsCancellationRequested)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
@ -203,7 +198,6 @@ namespace MapControl.Caching
SetAccessControl(path);
}
}
catch (Exception ex)
{
Debug.WriteLine($"ImageFileCache: Failed writing {path}: {ex.Message}");
@ -238,13 +232,25 @@ namespace MapControl.Caching
{
Debug.WriteLine($"ImageFileCache: Failed deleting {path}: {ex.Message}");
}
}
public Task RemoveAsync(string key, CancellationToken token = default)
public async Task RemoveAsync(string key, CancellationToken token = default)
{
Remove(key);
return Task.CompletedTask;
await memoryCache.RemoveAsync(key, token);
var path = GetPath(key);
try
{
if (path != null && File.Exists(path) && !token.IsCancellationRequested)
{
File.Delete(path);
}
}
catch (Exception ex)
{
Debug.WriteLine($"ImageFileCache: Failed deleting {path}: {ex.Message}");
}
}
private string GetPath(string key)