forked from linkdotnet/BlogExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedRepository.cs
More file actions
31 lines (25 loc) · 764 Bytes
/
CachedRepository.cs
File metadata and controls
31 lines (25 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using Microsoft.Extensions.Caching.Memory;
namespace DecoratorPattern;
public class CachedRepository : IRepository
{
private readonly IMemoryCache _memoryCache;
private readonly IRepository _repository;
public CachedRepository(IMemoryCache memoryCache, IRepository repository)
{
_memoryCache = memoryCache;
_repository = repository;
}
public async Task<Person> GetPersonByIdAsync(int id)
{
if (!_memoryCache.TryGetValue(id, out Person value))
{
value = await _repository.GetPersonByIdAsync(id);
_memoryCache.Set(id, value);
}
return value;
}
public Task SavePersonAsync(Person person)
{
return _repository.SavePersonAsync(person);
}
}