2024-12-13 10:54:47 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using Persistence.Models;
|
|
|
|
|
|
|
|
|
|
namespace Persistence.Repository.Repositories;
|
|
|
|
|
public class DataSourceSystemCachedRepository : DataSourceSystemRepository
|
|
|
|
|
{
|
|
|
|
|
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey";
|
|
|
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
|
private const int CacheExpirationInMinutes = 60;
|
2024-12-13 15:30:11 +05:00
|
|
|
|
private readonly TimeSpan? AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60);
|
2024-12-13 10:54:47 +05:00
|
|
|
|
|
|
|
|
|
public DataSourceSystemCachedRepository(DbContext db, IMemoryCache memoryCache) : base(db)
|
|
|
|
|
{
|
|
|
|
|
this.memoryCache = memoryCache;
|
|
|
|
|
}
|
|
|
|
|
public override async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await base.Add(dataSourceSystemDto, token);
|
|
|
|
|
|
|
|
|
|
memoryCache.Remove(SystemCacheKey);
|
|
|
|
|
}
|
|
|
|
|
public override async Task<IEnumerable<DataSourceSystemDto>> Get(CancellationToken token)
|
|
|
|
|
{
|
2024-12-13 15:30:11 +05:00
|
|
|
|
var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async (cacheEntry) =>
|
2024-12-13 10:54:47 +05:00
|
|
|
|
{
|
2024-12-13 15:30:11 +05:00
|
|
|
|
cacheEntry.AbsoluteExpirationRelativeToNow = AbsoluteExpirationRelativeToNow;
|
|
|
|
|
|
2024-12-13 10:54:47 +05:00
|
|
|
|
var dtos = await base.Get(token);
|
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return systems ?? [];
|
|
|
|
|
}
|
|
|
|
|
}
|