mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-01-04 22:54:20 +01:00
Refactoring: Do not query database when asking for Coinaverage rates, periodically get exchange list
This commit is contained in:
@@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using BTCPayServer.Models;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
|
||||
using Newtonsoft.Json;
|
||||
using System.Threading;
|
||||
|
||||
namespace BTCPayServer.Services
|
||||
{
|
||||
@@ -51,6 +52,22 @@ namespace BTCPayServer.Services
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
IReadOnlyCollection<TaskCompletionSource<bool>> value;
|
||||
lock (_Subscriptions)
|
||||
{
|
||||
if(_Subscriptions.TryGetValue(typeof(T), out value))
|
||||
{
|
||||
_Subscriptions.Remove(typeof(T));
|
||||
}
|
||||
}
|
||||
if(value != null)
|
||||
{
|
||||
foreach(var v in value)
|
||||
{
|
||||
v.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private T Deserialize<T>(string value)
|
||||
@@ -62,5 +79,35 @@ namespace BTCPayServer.Services
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj);
|
||||
}
|
||||
|
||||
MultiValueDictionary<Type, TaskCompletionSource<bool>> _Subscriptions = new MultiValueDictionary<Type, TaskCompletionSource<bool>>();
|
||||
public async Task WaitSettingsChanged<T>(CancellationToken cancellation)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
using (cancellation.Register(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
catch { }
|
||||
}))
|
||||
{
|
||||
lock (_Subscriptions)
|
||||
{
|
||||
_Subscriptions.Add(typeof(T), tcs);
|
||||
}
|
||||
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
|
||||
tcs.Task.ContinueWith(_ =>
|
||||
{
|
||||
lock (_Subscriptions)
|
||||
{
|
||||
_Subscriptions.Remove(typeof(T), tcs);
|
||||
}
|
||||
}, TaskScheduler.Default);
|
||||
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
|
||||
await tcs.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user