Add editorconfig and analyzers, format backend code

This commit is contained in:
2026-09-24 04:54:17 +02:00
parent 87c3f97529
commit c8fe28b07b
367 changed files with 12179 additions and 12459 deletions
@@ -2,40 +2,39 @@ using System.Collections.Concurrent;
using ErrorOr;
using Microsoft.Extensions.Caching.Memory;
namespace Musify.Infrastructure.Services
namespace Musify.Infrastructure.Services;
public sealed class SingleFlightCache(IMemoryCache cache)
{
public sealed class SingleFlightCache(IMemoryCache cache)
private readonly ConcurrentDictionary<string, object> inFlight = new();
public async Task<ErrorOr<TValue>> GetOrCreateAsync<TValue>(
string key,
TimeSpan duration,
Func<Task<ErrorOr<TValue>>> factory)
{
private readonly ConcurrentDictionary<string, object> inFlight = new();
if (duration <= TimeSpan.Zero)
return await factory();
public async Task<ErrorOr<TValue>> GetOrCreateAsync<TValue>(
string key,
TimeSpan duration,
Func<Task<ErrorOr<TValue>>> factory)
if (cache.TryGetValue(key, out TValue? cached) && cached != null)
return cached;
var pending = (Lazy<Task<ErrorOr<TValue>>>)inFlight.GetOrAdd(
key,
_ => new Lazy<Task<ErrorOr<TValue>>>(factory));
try
{
if (duration <= TimeSpan.Zero)
return await factory();
var result = await pending.Value;
if (cache.TryGetValue(key, out TValue? cached) && cached != null)
return cached;
if (!result.IsError)
cache.Set(key, result.Value, duration);
var pending = (Lazy<Task<ErrorOr<TValue>>>)inFlight.GetOrAdd(
key,
_ => new Lazy<Task<ErrorOr<TValue>>>(factory));
try
{
var result = await pending.Value;
if (!result.IsError)
cache.Set(key, result.Value, duration);
return result;
}
finally
{
inFlight.TryRemove(key, out _);
}
return result;
}
finally
{
inFlight.TryRemove(key, out _);
}
}
}