-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Dapper; | ||
|
||
namespace Tzkt.Api.Services.Cache | ||
{ | ||
public class QuotesCache : DbConnection | ||
{ | ||
readonly List<double>[] Quotes; | ||
readonly ILogger Logger; | ||
|
||
public QuotesCache(IConfiguration config, ILogger<QuotesCache> logger) : base(config) | ||
{ | ||
Quotes = new List<double>[3]; | ||
Logger = logger; | ||
|
||
Logger.LogDebug("Initializing quotes cache..."); | ||
|
||
var sql = @" | ||
SELECT ""Btc"", ""Eur"", ""Usd"" | ||
FROM ""Quotes"" | ||
ORDER BY ""Level"""; | ||
|
||
using var db = GetConnection(); | ||
var rows = db.Query(sql); | ||
|
||
Quotes[0] = rows.Select(x => (double)x.Btc).ToList(); | ||
Quotes[1] = rows.Select(x => (double)x.Eur).ToList(); | ||
Quotes[2] = rows.Select(x => (double)x.Usd).ToList(); | ||
|
||
Logger.LogDebug($"Quotes cache initialized with {Quotes[0].Count} items"); | ||
} | ||
|
||
public double Get(int symbol) | ||
{ | ||
return Quotes[symbol][^1]; | ||
} | ||
|
||
public double Get(int symbol, int level) | ||
{ | ||
return level >= Quotes[symbol].Count ? Quotes[symbol][^1] : Quotes[symbol][level]; | ||
} | ||
|
||
public async Task UpdateAsync() | ||
{ | ||
var sql = $@" | ||
SELECT ""Btc"", ""Eur"", ""Usd"" | ||
FROM ""Quotes"" | ||
WHERE ""Level"" >= {Quotes[0].Count} | ||
ORDER BY ""Level"""; | ||
|
||
using var db = GetConnection(); | ||
var rows = await db.QueryAsync(sql); | ||
|
||
Quotes[0].AddRange(rows.Select(x => (double)x.Btc)); | ||
Quotes[1].AddRange(rows.Select(x => (double)x.Eur)); | ||
Quotes[2].AddRange(rows.Select(x => (double)x.Usd)); | ||
} | ||
} | ||
|
||
public static class QuotesCacheExt | ||
{ | ||
public static void AddQuotesCache(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<QuotesCache>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters