Add RPC call caching
ADD
.vscode/launch.json
+{
+ // Use IntelliSense to find out which attributes exist for C# debugging
+ // Use hover for the description of the existing attributes
+ // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": ".NET Core Launch (web)",
+ "type": "coreclr",
+ "request": "launch",
+ "preLaunchTask": "build",
+ // If you have changed target frameworks, make sure to update the program path.
+ "program": "${workspaceRoot}/MagicalCryptoWallet.Backend/bin/Debug/netcoreapp2.0/MagicalCryptoWallet.Backend.dll",
+ "args": [],
+ "cwd": "${workspaceRoot}/MagicalCryptoWallet.Backend",
+ "stopAtEntry": false,
+ "internalConsoleOptions": "openOnSessionStart",
+ "launchBrowser": {
+ "enabled": true,
+ "args": "${auto-detect-url}",
+ "windows": {
+ "command": "cmd.exe",
+ "args": "/C start ${auto-detect-url}"
+ },
+ "osx": {
+ "command": "open"
+ },
+ "linux": {
+ "command": "xdg-open"
+ }
+ },
+ "env": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ },
+ "sourceFileMap": {
+ "/Views": "${workspaceRoot}/Views"
+ }
+ },
+ {
+ "name": ".NET Core Attach",
+ "type": "coreclr",
+ "request": "attach",
+ "processId": "${command:pickProcess}"
+ }
+ ]
+}
\ No newline at end of file

ADD
.vscode/tasks.json
+{
+ "version": "0.1.0",
+ "command": "dotnet",
+ "isShellCommand": true,
+ "args": [],
+ "tasks": [
+ {
+ "taskName": "build",
+ "args": [
+ "${workspaceRoot}/MagicalCryptoWallet.Backend/MagicalCryptoWallet.Backend.csproj"
+ ],
+ "isBuildCommand": true,
+ "problemMatcher": "$msCompile"
+ }
+ ]
+}
\ No newline at end of file

MODIFY
MagicalCryptoWallet.Backend/Controllers/BlockchainController.cs
MagicalCryptoWallet.Backend/Controllers/BlockchainController.cs
@@ -68,6 +68,7 @@ namespace MagicalCryptoWallet.Backend.Controllers
}
var feeEstimations = new SortedDictionary<int, FeeEstimationPair>();
+
foreach (int target in confirmationTargetsInts)
{
if (Network != Network.RegTest)
@@ -76,8 +77,8 @@ namespace MagicalCryptoWallet.Backend.Controllers
// 1. Use the sanity check that under 5 satoshi per bytes should not be displayed.
// 2. Use the RPCResponse.Blocks output to avoid redundant RPC queries.
// 3. Implement caching.
- var conservativeResponse = await RpcClient.EstimateSmartFeeAsync(target, EstimateSmartFeeMode.Conservative);
- var economicalResponse = await RpcClient.EstimateSmartFeeAsync(target, EstimateSmartFeeMode.Economical);
+ var conservativeResponse = await GetEstimateSmartFeeAsync(target, EstimateSmartFeeMode.Conservative);
+ var economicalResponse = await GetEstimateSmartFeeAsync(target, EstimateSmartFeeMode.Economical);
var conservativeFee = conservativeResponse.FeeRate.FeePerK.Satoshi / 1000;
var economicalFee = economicalResponse.FeeRate.FeePerK.Satoshi / 1000;
@@ -213,5 +214,24 @@ namespace MagicalCryptoWallet.Backend.Controllers
return Ok(filters);
}
+
+ private async Task<EstimateSmartFeeResponse> GetEstimateSmartFeeAsync(int target, EstimateSmartFeeMode mode)
+ {
+ EstimateSmartFeeResponse feeResponse=null;
+
+ var cacheKey = $"{nameof(GetEstimateSmartFeeAsync)}_{target}_{Enum.GetName(typeof(EstimateSmartFeeMode), mode)}";
+
+ if (!_cache.TryGetValue(cacheKey, out feeResponse))
+ {
+ feeResponse = await RpcClient.EstimateSmartFeeAsync(target, mode);
+
+ var cacheEntryOptions = new MemoryCacheEntryOptions()
+ .SetAbsoluteExpiration(TimeSpan.FromSeconds(20));
+
+ _cache.Set(cacheKey, feeResponse, cacheEntryOptions);
+ }
+
+ return feeResponse;
+ }
}
}
\ No newline at end of file

Add RPC call caching