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