Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read cache settings from env var #31

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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