btcpayserver/BTCPayServer/Plugins/CoinSwitch/CoinSwitchService.cs
Andrew Camilleri 64e34d0ef5
Seaparate CoinSwitch as a plugin (#2390)
* Separate coinswitch as a system plugin

* Decouple Coinswitch from Checkout UI

* remove dummy csproj

* Remove CoinSwitchTests.cs per @NicolasDorier feedback

Co-authored-by: rockstardev <rockstardev@users.noreply.github.com>
2021-04-13 20:19:48 +09:00

37 lines
1.1 KiB
C#

using System;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Services.Stores;
using Microsoft.Extensions.Caching.Memory;
namespace BTCPayServer.Plugins.CoinSwitch
{
public class CoinSwitchService: IDisposable
{
private readonly StoreRepository _storeRepository;
private readonly IMemoryCache _memoryCache;
public CoinSwitchService(StoreRepository storeRepository, IMemoryCache memoryCache)
{
_storeRepository = storeRepository;
_memoryCache = memoryCache;
}
public async Task<CoinSwitchSettings> GetCoinSwitchForInvoice(string id)
{
return await _memoryCache.GetOrCreateAsync($"{nameof(CoinSwitchService)}-{id}", async entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
var d = await _storeRepository.GetStoreByInvoiceId(id);
return d?.GetStoreBlob()?.GetCoinSwitchSettings();
});
}
public void Dispose()
{
_memoryCache?.Dispose();
}
}
}