Skip to content

Commit

Permalink
Add output cache for /accounts, /operations, and /tokens/balances end…
Browse files Browse the repository at this point in the history
…points
  • Loading branch information
dmirgaleev authored Jun 30, 2022
1 parent c2f9c9b commit a57b068
Show file tree
Hide file tree
Showing 24 changed files with 1,364 additions and 345 deletions.
190 changes: 150 additions & 40 deletions Tzkt.Api/Controllers/AccountsController.cs

Large diffs are not rendered by default.

1,116 changes: 851 additions & 265 deletions Tzkt.Api/Controllers/OperationsController.cs

Large diffs are not rendered by default.

45 changes: 35 additions & 10 deletions Tzkt.Api/Controllers/TokensController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Mvc;
using Tzkt.Api.Models;
using Tzkt.Api.Repositories;
using Tzkt.Api.Services;
using Tzkt.Api.Services.Cache;

namespace Tzkt.Api.Controllers
Expand All @@ -14,11 +15,13 @@ public class TokensController : ControllerBase
{
readonly TokensRepository Tokens;
readonly StateCache State;
readonly ResponseCacheService ResponseCache;

public TokensController(TokensRepository tokens, StateCache state)
public TokensController(TokensRepository tokens, StateCache state, ResponseCacheService responseCache)
{
Tokens = tokens;
State = state;
ResponseCache = responseCache;
}

#region tokens
Expand Down Expand Up @@ -84,7 +87,7 @@ public async Task<ActionResult<IEnumerable<Token>>> GetTokens(
/// <param name="filter">Filter</param>
/// <returns></returns>
[HttpGet("balances/count")]
public Task<int> GetTokenBalancesCount([FromQuery] TokenBalanceFilter filter)
public async Task<ActionResult<int>> GetTokenBalancesCount([FromQuery] TokenBalanceFilter filter)
{
if (filter.account != null ||
filter.balance != null ||
Expand All @@ -98,9 +101,18 @@ public Task<int> GetTokenBalancesCount([FromQuery] TokenBalanceFilter filter)
filter.token.tokenId != null ||
filter.token.standard != null ||
filter.token.metadata != null)
return Tokens.GetTokenBalancesCount(filter);
{
var query = ResponseCacheService.BuildKey(Request.Path.Value, ("filter", filter));

if (ResponseCache.TryGet(query, out var cached))
return this.Bytes(cached);

var res = await Tokens.GetTokenBalancesCount(filter);
cached = ResponseCache.Set(query, res);
return this.Bytes(cached);
}

return Task.FromResult(State.Current.TokenBalancesCount);
return Ok(State.Current.TokenBalancesCount);
}

/// <summary>
Expand All @@ -119,14 +131,27 @@ public async Task<ActionResult<IEnumerable<TokenBalance>>> GetTokenBalances(
[FromQuery] Pagination pagination,
[FromQuery] Selection selection)
{
if (selection.select == null)
return Ok(await Tokens.GetTokenBalances(filter, pagination));
var query = ResponseCacheService.BuildKey(Request.Path.Value,
("filter", filter), ("pagination", pagination), ("selection", selection));

return Ok(new SelectionResponse
if (ResponseCache.TryGet(query, out var cached))
return this.Bytes(cached);

object res;
if (selection.select == null)
{
Cols = selection.select.Fields?.Select(x => x.Alias).ToArray(),
Rows = await Tokens.GetTokenBalances(filter, pagination, selection.select.Fields ?? selection.select.Values)
});
res = await Tokens.GetTokenBalances(filter, pagination);
}
else
{
res = new SelectionResponse
{
Cols = selection.select.Fields?.Select(x => x.Alias).ToArray(),
Rows = await Tokens.GetTokenBalances(filter, pagination, selection.select.Fields ?? selection.select.Values)
};
}
cached = ResponseCache.Set(query, res);
return this.Bytes(cached);
}
#endregion

Expand Down
17 changes: 15 additions & 2 deletions Tzkt.Api/Parameters/AccountTypeParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

namespace Tzkt.Api
Expand Down Expand Up @@ -28,7 +29,19 @@ public class AccountTypeParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Ne != null)
{
sb.Append($"{name}.ne={Ne}&");
}

return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion Tzkt.Api/Parameters/AnyOfParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AnyOfParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
return $"{name}.{string.Join(".", Fields)}={Value}&";
}
}
}
17 changes: 15 additions & 2 deletions Tzkt.Api/Parameters/BakingRightStatusParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

namespace Tzkt.Api
Expand Down Expand Up @@ -28,7 +29,19 @@ public class BakingRightStatusParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Ne != null)
{
sb.Append($"{name}.ne={Ne}&");
}

return sb.ToString();
}
}
}
17 changes: 15 additions & 2 deletions Tzkt.Api/Parameters/BakingRightTypeParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

namespace Tzkt.Api
Expand Down Expand Up @@ -28,7 +29,19 @@ public class BakingRightTypeParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Ne != null)
{
sb.Append($"{name}.ne={Ne}&");
}

return sb.ToString();
}
}
}
17 changes: 15 additions & 2 deletions Tzkt.Api/Parameters/BoolParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

namespace Tzkt.Api
Expand All @@ -25,7 +26,19 @@ public class BoolParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Null != null)
{
sb.Append($"{name}.null={Null}&");
}

return sb.ToString();
}
}
}
46 changes: 45 additions & 1 deletion Tzkt.Api/Parameters/DateTimeParameter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

Expand Down Expand Up @@ -75,7 +77,49 @@ public class DateTimeParameter : INormalizable

public string Normalize(string name)
{
throw new NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Ne != null)
{
sb.Append($"{name}.ne={Ne}&");
}

if (Gt != null)
{
sb.Append($"{name}.gt={Gt}&");
}

if (Ge != null)
{
sb.Append($"{name}.ge={Ge}&");
}

if (Lt != null)
{
sb.Append($"{name}.lt={Lt}&");
}

if (Le != null)
{
sb.Append($"{name}.le={Le}&");
}

if (In?.Count > 0)
{
sb.Append($"{name}.in={string.Join(",", In.OrderBy(x => x))}&");
}

if (Ni?.Count > 0)
{
sb.Append($"{name}.ni={string.Join(",", Ni.OrderBy(x => x))}&");
}

return sb.ToString();
}
}
}
26 changes: 25 additions & 1 deletion Tzkt.Api/Parameters/ExpressionParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

Expand Down Expand Up @@ -46,7 +48,29 @@ public class ExpressionParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Ne != null)
{
sb.Append($"{name}.ne={Ne}&");
}

if (In?.Count > 0)
{
sb.Append($"{name}.in={string.Join(",", In.OrderBy(x => x))}&");
}

if (Ni?.Count > 0)
{
sb.Append($"{name}.ni={string.Join(",", Ni.OrderBy(x => x))}&");
}

return sb.ToString();
}
}
}
26 changes: 25 additions & 1 deletion Tzkt.Api/Parameters/MigrationKindParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

Expand Down Expand Up @@ -47,7 +49,29 @@ public class MigrationKindParameter : INormalizable

public string Normalize(string name)
{
throw new System.NotImplementedException();
var sb = new StringBuilder();

if (Eq != null)
{
sb.Append($"{name}.eq={Eq}&");
}

if (Ne != null)
{
sb.Append($"{name}.ne={Ne}&");
}

if (In?.Count > 0)
{
sb.Append($"{name}.in={string.Join(",", In.OrderBy(x => x))}&");
}

if (Ni?.Count > 0)
{
sb.Append($"{name}.ni={string.Join(",", Ni.OrderBy(x => x))}&");
}

return sb.ToString();
}
}
}
Loading

0 comments on commit a57b068

Please sign in to comment.