Skip to content

Commit

Permalink
feat: read cache settings from env var (#31)
Browse files Browse the repository at this point in the history
Description
---
Read cache related settings on the start from env var (with some default
values).

Motivation and Context
---

How Has This Been Tested?
---
See the cache settings, try to change the env var and rerun to see the
effect.

What process can a PR reviewer use to test or verify this change?
---

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
leet4tari authored Feb 28, 2024
2 parents d19948c + 1545fe4 commit c3cfc8f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
18 changes: 18 additions & 0 deletions cacheSettings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var cacheSettings = {
index:
process.env.TARI_EXPLORER_INDEX_CACHE_SETTINGS ||
"public, max-age=120, s-maxage=60, stale-while-revalidate=30",
mempool:
process.env.TARI_EXPLORER_MEMPOOL_CACHE_SETTINGS ||
"public, max-age=15, s-maxage=15, stale-while-revalidate=15",
oldBlocks:
process.env.TARI_EXPLORER_OLD_BLOCKS_CACHE_SETTINGS ||
"public, max-age=604800, s-maxage=604800, stale-while-revalidate=604800",
newBlocks:
process.env.TARI_EXPLORER_NEW_BLOCKS_CACHE_SETTINGS ||
"public, max-age=120, s-maxage=60, stale-while-revalidate=30",
oldBlockDeltaTip:
process.env.TARI_EXPLORER_OLD_BLOCK_DELTA_TIP || 30 * 24 * 7,
};

module.exports = cacheSettings;
1 change: 0 additions & 1 deletion routes/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ router.get("/:asset_public_key", async function (req, res) {
let tokens = await baseNodeClient.getTokens({
asset_public_key: Buffer.from(asset_public_key, "hex"),
});
console.log(tokens);

if (!tokens || tokens.length === 0) {
res.status(404);
Expand Down
8 changes: 7 additions & 1 deletion routes/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var { createClient } = require("../baseNodeClient");

var express = require("express");
const cache = require("../cache");
const cacheSettings = require("../cacheSettings");
var router = express.Router();

function fromHexString(hexString) {
Expand All @@ -35,7 +36,6 @@ function fromHexString(hexString) {
}

router.get("/:height_or_hash", async function (req, res) {
res.setHeader("Cache-Control", "public, max-age=120, immutable");
try {
let client = createClient();
let height_or_hash = req.params.height_or_hash;
Expand Down Expand Up @@ -213,6 +213,12 @@ router.get("/:height_or_hash", async function (req, res) {
let nextLink = `/blocks/${nextHeight}`;
if (height === tipHeight) nextLink = null;

if (height + cacheSettings.oldBlockDeltaTip <= tipHeight) {
res.setHeader("Cache-Control", cacheSettings.oldBlocks);
} else {
res.setHeader("Cache-Control", cacheSettings.newBlocks);
}

let json = {
title: `Block at height: ${block[0].block.header.height}`,
header: block[0].block.header,
Expand Down
7 changes: 2 additions & 5 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
var { createClient } = require("../baseNodeClient");

var express = require("express");
const cacheSettings = require("../cacheSettings");
var router = express.Router();

/* GET home page. */
router.get("/", async function (req, res) {
res.setHeader("Cache-Control", cacheSettings.index);
try {
let client = createClient();
let from = parseInt(req.query.from || 0);
Expand All @@ -27,8 +29,6 @@ router.get("/", async function (req, res) {
let monero = [0, 0, 0, 0];
let sha = [0, 0, 0, 0];

// console.log(last100Headers)

for (let i = 0; i < last100Headers.length - 1; i++) {
let arr = last100Headers[i].pow.pow_algo === "0" ? monero : sha;
if (i < 10) {
Expand All @@ -53,7 +53,6 @@ router.get("/", async function (req, res) {
sha100: sha[3],
};

// console.log(algoSplit)
// Get one more header than requested so we can work out the difference in MMR_size
let headersResp = await client.listHeaders({
from_height: from,
Expand All @@ -78,7 +77,6 @@ router.get("/", async function (req, res) {
headers.splice(headers.length - 1, 1);
}

// console.log(headers);
let firstHeight = parseInt(headers[0].height || "0");

// -- mempool
Expand All @@ -104,7 +102,6 @@ router.get("/", async function (req, res) {
height: tipHeight,
});

// console.log(mempool);
for (let i = 0; i < mempool.length; i++) {
let sum = 0;
for (let j = 0; j < mempool[i].transaction.body.kernels.length; j++) {
Expand Down
6 changes: 2 additions & 4 deletions routes/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

var express = require("express");
const { createClient } = require("../baseNodeClient");
const cacheSettings = require("../cacheSettings");
var router = express.Router();

/* GET mempool page. */
router.get("/:excessSigs", async function (req, res) {
res.setHeader("Cache-Control", "public, max-age=0, must-revalidate");
res.setHeader("Cache-Control", cacheSettings.mempool);
try {
let client = createClient();
let txId = req.params.excessSigs.split("+");
console.log(txId);
let mempool = await client.getMempoolTransactions({});
let tx = null;
for (let i = 0; i < mempool.length; i++) {
Expand Down Expand Up @@ -42,8 +42,6 @@ router.get("/:excessSigs", async function (req, res) {
}
return;
}
console.log(tx);
console.log("===============");
let json = { tx };
if (req.query.json !== undefined) {
res.json(json);
Expand Down
3 changes: 2 additions & 1 deletion routes/search_commitments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
var { createClient } = require("../baseNodeClient");

var express = require("express");
const cacheSettings = require("../cacheSettings");
var router = express.Router();

router.get("/", async function (req, res) {
res.setHeader("Cache-Control", "public, max-age=120, immutable");
res.setHeader("Cache-Control", cacheSettings.newBlocks);
let client = createClient();
let commitments = (
req.query.comm ||
Expand Down
3 changes: 2 additions & 1 deletion routes/search_kernels.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
var { createClient } = require("../baseNodeClient");

var express = require("express");
const cacheSettings = require("../cacheSettings");
var router = express.Router();

router.get("/", async function (req, res) {
res.setHeader("Cache-Control", "public, max-age=120, immutable");
res.setHeader("Cache-Control", cacheSettings.newBlocks);
let client = createClient();
let nonces = (req.query.nonces || "").split(",");
let signatures = (req.query.signatures || "").split(",");
Expand Down

0 comments on commit c3cfc8f

Please sign in to comment.