using DirectService.Tools; using System; using System.Data.SqlClient; namespace LJLib { public class CacheService : ICacheService where TValue : class, new() { private readonly LJCache _cache; private readonly ICacheLoader _loader; public CacheService(ICacheLoader loader, int cacheMinutes = 120) { _loader = loader ?? throw new ArgumentNullException(nameof(loader)); _cache = new LJCache { DefaultAddMinutes = cacheMinutes }; } public TValue Get(SqlCommand cmd, TKey key) { if (!_cache.TryGetValue(key, out TValue value)) { value = _loader.Load(cmd, key); _cache.Add(key, value); } return ObjectHelper.DeepCopy(value); } } }