using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using Microsoft.EntityFrameworkCore; using OliverBooth.Data.Blog; namespace OliverBooth.Services; /// /// Represents an implementation of . /// internal sealed class BlogUserService : IBlogUserService { private readonly IDbContextFactory _dbContextFactory; private readonly ConcurrentDictionary _userCache = new(); /// /// Initializes a new instance of the class. /// /// /// The used to create a . /// public BlogUserService(IDbContextFactory dbContextFactory) { _dbContextFactory = dbContextFactory; } /// public bool TryGetUser(Guid id, [NotNullWhen(true)] out IUser? user) { if (_userCache.TryGetValue(id, out user)) return true; using BlogContext context = _dbContextFactory.CreateDbContext(); user = context.Users.Find(id); if (user is not null) _userCache.TryAdd(id, user); return user is not null; } }