using ECMJobRunner.Application.Common.Dtos;
using System.Collections.Concurrent;
namespace ECMJobRunner.WebCron.ProfileWorker;
///
/// Thread-safe cache for storing active profile configurations.
/// Uses to track profile state
/// and detect changes in schedule or removal.
///
public class ProfileCache
{
private readonly ConcurrentDictionary _cache = new();
///
/// Retrieves a profile from the cache by job identifier.
///
/// The unique job identifier.
/// The cached profile, or null if not found.
public CfgProfileDto? Get(string jobId) => _cache.TryGetValue(jobId, out var profile) ? profile : null;
///
/// Adds a new profile or updates an existing profile in the cache.
///
/// The unique job identifier.
/// The profile configuration to cache.
public void AddOrUpdate(string jobId, CfgProfileDto profile) => _cache[jobId] = profile;
///
/// Attempts to remove a profile from the cache.
///
/// The unique job identifier.
/// The removed profile, or null if not found.
/// true if the profile was removed; otherwise, false.
public bool TryRemove(string jobId, out CfgProfileDto? profile) => _cache.TryRemove(jobId, out profile);
///
/// Gets all job identifiers currently stored in the cache.
///
/// A collection of job identifiers.
public IEnumerable GetAllJobIds() => _cache.Keys;
///
/// Attempts to retrieve a profile from the cache.
///
/// The unique job identifier.
/// The cached profile, or null if not found.
/// true if the profile was found; otherwise, false.
public bool TryGetValue(string jobId, out CfgProfileDto? profile) => _cache.TryGetValue(jobId, out profile);
}