44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using GameData.Repository;
|
|
using GameModel;
|
|
using GameModel.Contract;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Caching;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameData.CacheProxy
|
|
{
|
|
/// <summary>
|
|
/// cache for repository data, implemented as proxy not as decorator https://www.youtube.com/watch?v=nmAE-JzNSEw
|
|
/// </summary>
|
|
public class CachedExpenseRepository : IExpenseRepository
|
|
{
|
|
IMemoryCache _cache;
|
|
ExpenseRepository _repository;
|
|
|
|
public CachedExpenseRepository(ExpenseRepository repository, IMemoryCache cache)
|
|
{
|
|
_repository = repository;
|
|
_cache = cache;
|
|
}
|
|
|
|
public Task Delete(IEnumerable<PlayerExpense> enumerable)
|
|
{
|
|
return _repository.Delete(enumerable);
|
|
}
|
|
|
|
public Task<IEnumerable<Expense>> GetAll()
|
|
{
|
|
return _cache.GetOrCreateAsync("allexpenses", factory => _repository.GetAll());
|
|
}
|
|
|
|
public Task<PlayerExpense> Save(PlayerExpense data)
|
|
{
|
|
return _repository.Save(data);
|
|
}
|
|
}
|
|
}
|