redis cache updated

This commit is contained in:
root 2019-08-21 15:33:54 +05:30
parent 045636da98
commit d73b8c1c73
6 changed files with 60 additions and 49 deletions

View File

@ -44,6 +44,7 @@
"moment": "^2.24.0", "moment": "^2.24.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"ngx-cookie-service": "^2.2.0", "ngx-cookie-service": "^2.2.0",
"node-cron": "^2.0.3",
"pg": "^7.11.0", "pg": "^7.11.0",
"pg-native": "^3.0.0", "pg-native": "^3.0.0",
"plotly.js": "^1.48.3", "plotly.js": "^1.48.3",

View File

@ -63,6 +63,7 @@ config({ path: resolve(__dirname, "../.env") });
// const connection = connectionManager.create(dbConfig); // const connection = connectionManager.create(dbConfig);
import { join } from "path"; import { join } from "path";
var cron = require('node-cron');
// Faster server renders w/ Prod mode (dev mode never needed) // Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode(); enableProdMode();
@ -111,6 +112,8 @@ app.get("/swagger.json", function(req, res) {
); );
}); });
controllers.forEach(controller => { controllers.forEach(controller => {
app.use("/epic_explorer/v1", controller.router); app.use("/epic_explorer/v1", controller.router);
}); });
@ -185,12 +188,22 @@ console.log(__dirname);
const server = app.listen(PORT, () => { const server = app.listen(PORT, () => {
console.log(`Node Express server listening on http://localhost:${PORT}`); console.log(`Node Express server listening on http://localhost:${PORT}`);
}); });
cron.schedule('* * * * * *', () => {
universalGetLatestBlockDetails('Testnet');
universalGetLatestBlockDetails('Floonet');
});
const io = require("socket.io").listen(server); const io = require("socket.io").listen(server);
io.sockets.on("connection", socket => { io.sockets.on("connection", socket => {
//setTimeout(function() { //console.log(socket.handshake.query.network);
//universalGetLatestBlockDetails(socket); //var network = "Testnet";
//},1000); let key = process.env.REDIS_KEY + socket.handshake.query.network + 'Latest_Block_details'
socket.on("disconnect", () => console.log("Client disconnected")); setInterval(function() {
Global.client.get(key, function(err, reply){
socket.emit("latestblockdetail", JSON.parse(reply) );
});
},1000);
//socket.on("disconnect", () => console.log("Client disconnected"));
}); });
}) })
.catch(error => { .catch(error => {

View File

@ -1,3 +1,7 @@
import * as redis from 'redis';
var REDIS_URL = process.env.REDIS_URL;
export namespace Global { export namespace Global {
export var network: string = 'Floonet'; export var network: string = 'Floonet';
// connect to Redis
export var client = redis.createClient(REDIS_URL);
} }

View File

@ -5,35 +5,35 @@ import { Global } from "../global";
import { Duration } from 'moment'; import { Duration } from 'moment';
// connect to Redis // connect to Redis
const REDIS_URL = process.env.REDIS_URL; // const REDIS_URL = process.env.REDIS_URL;
const client = redis.createClient(REDIS_URL); // const client = redis.createClient(REDIS_URL);
client.on('connect', () => { Global.client.on('connect', () => {
console.log(`connected to redis`); console.log(`connected to redis`);
}); });
client.on('error', err => { Global.client.on('error', err => {
console.log(`Error: ${err}`); console.log(`Error: ${err}`);
}); });
export function redisMiddleware( export function redisMiddleware(
duration: any, duration: any,
) { ) {
console.log(duration);
return (request, response, next) => { return (request, response, next) => {
//console.log(request.originalUrl);
// Global.network = request.headers.network; // Global.network = request.headers.network;
let key = process.env.REDIS_KEY + Global.network + request.originalUrl || request.url let key = process.env.REDIS_KEY + Global.network + request.originalUrl || request.url
client.get(key, function(err, reply){ Global.client.get(key, function(err, reply){
if(reply && duration!=0){ if(reply && duration!=0){
console.log("key : ", key); //console.log("key : ", key);
console.log("reply : ", reply); //console.log("reply : ", reply);
console.log("---------------------------------------------------------------------------------------"); //console.log("---------------------------------------------------------------------------------------");
console.log(`enabled`); //console.log(`enabled`);
response.send(reply); response.send(JSON.parse(reply));
}else{ }else{
console.log(`raw`); //console.log(`raw`);
response.sendResponse = response.send; response.sendResponse = response.send;
response.send = (body) => { response.send = (body) => {
client.set(key, JSON.stringify(body), 'EX', duration, function(err){ Global.client.set(key, JSON.stringify(body), 'EX', duration, function(err){
//client.set(key, JSON.stringify(body)); //client.set(key, JSON.stringify(body));
response.sendResponse(body); response.sendResponse(body);
}); });

View File

@ -1,6 +1,5 @@
import { getConnection } from "typeorm"; import { getConnection } from "typeorm";
import { Global } from "../global"; import { Global } from "../global";
import {createConnection, Connection} from "typeorm";
var moment = require("moment"); var moment = require("moment");
moment.updateLocale('en', { moment.updateLocale('en', {
@ -71,35 +70,25 @@ function convertMinsToHrmin(millseconds, insec) {
} }
export async function universalGetLatestBlockDetails(socket) { export async function universalGetLatestBlockDetails(current_network) {
let key = process.env.REDIS_KEY + current_network + 'Latest_Block_details'
let block_height = "", let block_height = "",
letest_block, letest_block,
letest_block_num = "", letest_block_num = "",
letest_block_duration = ""; letest_block_duration = "";
// const BlockchainLatestBlockQuery = await getConnection("Testnet").query( const BlockchainLatestBlockQuery = await getConnection(current_network).query(
// "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1"); "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1");
// const BlockchainPreviousBlockQuery = await getConnection("Testnet").query( const BlockchainPreviousBlockQuery = await getConnection(current_network).query(
// "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" +
// "'" + "'" +
// BlockchainLatestBlockQuery[0].previous_id + BlockchainLatestBlockQuery[0].previous_id +
// "'" "'"
// ); );
getConnection("Testnet").query(
"SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1").then(res => {
var BlockchainLatestBlockQuery = res;
//console.log(res);
getConnection("Testnet").query(
"SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" +
"'" +
BlockchainLatestBlockQuery[0].previous_id +
"'"
).then( res2 => {
var BlockchainPreviousBlockQuery = res2;
//console.log(BlockchainPreviousBlockQuery); //console.log(BlockchainPreviousBlockQuery);
let height = BlockchainLatestBlockQuery[0].height; let height = BlockchainLatestBlockQuery[0].height;
@ -279,7 +268,7 @@ export async function universalGetLatestBlockDetails(socket) {
let arr = balance.match(/.{1,6}/g); let arr = balance.match(/.{1,6}/g);
var hasharray = arr.map(i => '#' + i); var hasharray = arr.map(i => '#' + i);
socket.emit("latestblockdetail", { var final_result = {
block_height, block_height,
letest_block, letest_block,
letest_block_num, letest_block_num,
@ -309,14 +298,12 @@ export async function universalGetLatestBlockDetails(socket) {
BlockchainLatestBlockQuery[0].total_difficulty_progpow, BlockchainLatestBlockQuery[0].total_difficulty_progpow,
TotalDifficultyRandomx: TotalDifficultyRandomx:
BlockchainLatestBlockQuery[0].total_difficulty_randomx BlockchainLatestBlockQuery[0].total_difficulty_randomx
}); }
setTimeout(function() {
universalGetLatestBlockDetails(socket);
},1000);
});
});
Global.client.set(key, JSON.stringify(final_result), 'EX', parseInt(process.env.REDIS_EXPIRY), function(err){
//client.set(key, JSON.stringify(body));
});
//console.log(final_result);
} }

View File

@ -15,9 +15,15 @@ import * as io from "socket.io-client";
export class ChartService { export class ChartService {
private server = environment.domain; private server = environment.domain;
private socket; private socket;
private socketnetwork: any;
constructor(public http: HttpClient) { constructor(public http: HttpClient) {
this.socket = io.connect(this.server); if(localStorage.getItem('network') == null){
this.socketnetwork = "Testnet"
}else{
this.socketnetwork = localStorage.getItem('network')
}
this.socket = io.connect(this.server, {query: 'network='+this.socketnetwork});
} }
// public createSocketConnection() { // public createSocketConnection() {