MdpPullFieldMappingService.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Admin.NET.Plugin.AiDOP.Entity.DataPlatform;
  2. using Microsoft.Extensions.Caching.Memory;
  3. namespace Admin.NET.Plugin.AiDOP.DataPlatform;
  4. public sealed class MdpPullFieldMappingService : ITransient
  5. {
  6. private readonly ISqlSugarClient _db;
  7. private readonly IMemoryCache _cache;
  8. public MdpPullFieldMappingService(ISqlSugarClient db, IMemoryCache cache)
  9. {
  10. _db = db;
  11. _cache = cache;
  12. }
  13. public async Task<IReadOnlyList<MdpFieldMapping>> GetAsync(long entityId, CancellationToken cancellationToken = default)
  14. {
  15. var key = CacheKey(entityId);
  16. if (_cache.TryGetValue(key, out IReadOnlyList<MdpFieldMapping>? cached) && cached != null)
  17. return cached;
  18. var list = await _db.Queryable<MdpFieldMapping>()
  19. .Where(x => x.EntityId == entityId)
  20. .OrderBy(x => x.SortOrder)
  21. .OrderBy(x => x.Id)
  22. .ToListAsync(cancellationToken);
  23. IReadOnlyList<MdpFieldMapping> frozen = list;
  24. _cache.Set(key, frozen, TimeSpan.FromSeconds(30));
  25. return frozen;
  26. }
  27. public void Invalidate(long entityId) => _cache.Remove(CacheKey(entityId));
  28. public static string CacheKey(long entityId) => $"mdp:pull-map:{entityId}";
  29. }