| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | using OzonDomains.Models.AccessControl; |
| | | 3 | | using OzonRepositories.Data; |
| | | 4 | | |
| | | 5 | | namespace Services.DataServices; |
| | | 6 | | |
| | | 7 | | public class AccessProfileDataServices |
| | | 8 | | { |
| | | 9 | | private readonly AccessProfileRepository _repository; |
| | | 10 | | private readonly IMemoryCache _cache; |
| | | 11 | | |
| | 0 | 12 | | public AccessProfileDataServices(AccessProfileRepository repository, IMemoryCache cache) |
| | | 13 | | { |
| | 0 | 14 | | _repository = repository; |
| | 0 | 15 | | _cache = cache; |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task<List<AccessProfile>> GetAllAsync() => |
| | 0 | 19 | | await _repository.GetAllAsync(); |
| | | 20 | | |
| | | 21 | | public async Task<AccessProfile?> GetAccessProfileAsync(int id) |
| | | 22 | | { |
| | 0 | 23 | | string cacheKey = $"access_profile_{id}"; |
| | 0 | 24 | | return await _cache.GetOrCreateAsync(cacheKey, async entry => |
| | 0 | 25 | | { |
| | 0 | 26 | | entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10); |
| | 0 | 27 | | return await _repository.GetByIdAsync(id); |
| | 0 | 28 | | }); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public void InvalidateCache(int id) |
| | | 32 | | { |
| | 0 | 33 | | _cache.Remove($"access_profile_{id}"); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task<int> AddAsync(AccessProfile profile) => |
| | 0 | 37 | | await _repository.AddAsync(profile); |
| | | 38 | | |
| | | 39 | | public async Task<int> UpdateAsync(AccessProfile profile) |
| | | 40 | | { |
| | 0 | 41 | | InvalidateCache(profile.Id); |
| | 0 | 42 | | return await _repository.UpdateAsync(profile); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<int> DeleteAsync(AccessProfile profile) |
| | | 46 | | { |
| | 0 | 47 | | InvalidateCache(profile.Id); |
| | 0 | 48 | | return await _repository.DeleteAsync(profile); |
| | 0 | 49 | | } |
| | | 50 | | } |