Skip to content

Commit

Permalink
connect to redis once on app start
Browse files Browse the repository at this point in the history
  • Loading branch information
ewansheldon committed Jan 9, 2025
1 parent 9bff285 commit ce0e5aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
16 changes: 3 additions & 13 deletions src/pricing.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
const { createClient } = require('redis');
const { getNetworks } = require('./networks');

const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || '6379';

const redis = createClient({
url: `redis://${redisHost}:${redisPort}`
});
redis.on('error', err => console.log('Redis Client Error', err));
const { redisClient } = require('./redis');

const getTokenPrices = async (networkName, token) => {
const prices = await redis.ZRANGE(`prices:${networkName}:${token}`, 0, 47);
const prices = await redisClient.ZRANGE(`prices:${networkName}:${token}`, 0, 47);
return prices.map(priceData => {
const [ts, price] = priceData.split(':');
return { ts, price };
Expand All @@ -19,16 +11,14 @@ const getTokenPrices = async (networkName, token) => {

const getNetworkPrices = async (networkName) => {
const networkPrices = {};
await redis.connect();
const tokens = await redis.SMEMBERS(`tokens:${networkName}`);
const tokens = await redisClient.SMEMBERS(`tokens:${networkName}`);
for (let j = 0; j < tokens.length; j++) {
const token = tokens[j];
networkPrices[token] = {
decimals: '8',
prices: await getTokenPrices(networkName, token)
};
}
await redis.disconnect();
return networkPrices;
};

Expand Down
16 changes: 3 additions & 13 deletions src/rate.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
const { createClient } = require('redis');

const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || '6379';

const redis = createClient({
url: `redis://${redisHost}:${redisPort}`
});
redis.on('error', err => console.log('Redis Client Error', err));
const { redisClient } = require("./redis");

const limited = async ip => {
const key = `rateLimit:${ip}`
const reqLimit = 100;
if (!redis.isReady) await redis.connect();
const visits = await redis.INCR(key);
if (visits === 1) await redis.EXPIRE(key, 60);
await redis.disconnect();
const visits = await redisClient.INCR(key);
if (visits === 1) await redisClient.EXPIRE(key, 60);
return visits > reqLimit;
}

Expand Down
22 changes: 22 additions & 0 deletions src/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { createClient } = require('redis');

const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || '6379';

const redisClient = createClient({
url: `redis://${redisHost}:${redisPort}`
});
redisClient.on('error', err => console.log('Redis Client Error', err));

async function main() {
await redisClient.connect();
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

module.exports = {
redisClient
}
16 changes: 3 additions & 13 deletions src/transactions.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const { createClient } = require('redis');
const { redisClient } = require('./redis');
const { parseQueryParams } = require('./utils')

const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || '6379';

const redis = createClient({
url: `redis://${redisHost}:${redisPort}`
});
redis.on('error', err => console.log('Redis Client Error', err));

const vaultTransactionsAddress = url => {
const regex = /^\/transactions\/(?<address>0x[\w\d]*)(\?(?<queryParams>.*))?.*$/;
return url.match(regex) && url.match(regex).groups;
Expand Down Expand Up @@ -56,10 +48,8 @@ const getTransactions = async url => {
const end = start + limit - 1;
const REV = !(sort === 'asc');
const key = getTransactionsKey(address)
await redis.connect();
const transactionData = await redis.ZRANGE_WITHSCORES(key, start, end, {REV});
const count = await redis.ZCARD(key);
await redis.disconnect();
const transactionData = await redisClient.ZRANGE_WITHSCORES(key, start, end, {REV});
const count = await redisClient.ZCARD(key);
const transactions = formatTransactions(transactionData);
return {
data: transactions,
Expand Down

0 comments on commit ce0e5aa

Please sign in to comment.