Skip to content
Snippets Groups Projects
Commit 1a2dc40a authored by valzav's avatar valzav
Browse files

cache onchain data in server render

parent 712fbccb
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,7 @@ class Redis { ...@@ -28,7 +28,7 @@ class Redis {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
resolve(res[0] === '{' ? JSON.parse(res) : res); resolve(res && res.length > 0 && res[0] === '{' ? JSON.parse(res) : res);
} }
}); });
this.client[call_name].apply(this.client, args); this.client[call_name].apply(this.client, args);
......
import React from 'react'; import React from 'react';
import { renderToString } from 'react-dom/server'; import { renderToString } from 'react-dom/server';
import Tarantool from 'db/tarantool'; import Tarantool from 'db/tarantool';
import Redis from 'db/redis';
import ServerHTML from './server-html'; import ServerHTML from './server-html';
import universalRender from '../shared/UniversalRender'; import universalRender from '../shared/UniversalRender';
import models from 'db/models'; import models from 'db/models';
...@@ -84,7 +85,14 @@ async function appRender(ctx) { ...@@ -84,7 +85,14 @@ async function appRender(ctx) {
} }
} }
const { body, title, statusCode, meta } = await universalRender({location: ctx.request.url, store, offchain, ErrorPage, tarantool: Tarantool.instance()}); const { body, title, statusCode, meta } = await universalRender({
location: ctx.request.url,
store,
offchain,
ErrorPage,
tarantool: Tarantool.instance(),
redis: Redis.instance()
});
// Assets name are found in `webpack-stats` file // Assets name are found in `webpack-stats` file
const assets_filename = ROOT + (process.env.NODE_ENV === 'production' ? '/tmp/webpack-stats-prod.json' : '/tmp/webpack-stats-dev.json'); const assets_filename = ROOT + (process.env.NODE_ENV === 'production' ? '/tmp/webpack-stats-prod.json' : '/tmp/webpack-stats-dev.json');
......
...@@ -27,7 +27,6 @@ import Translator from 'app/Translator'; ...@@ -27,7 +27,6 @@ import Translator from 'app/Translator';
import {notificationsArrayToMap} from 'app/utils/Notifications'; import {notificationsArrayToMap} from 'app/utils/Notifications';
import {routeRegex} from "app/ResolveRoute"; import {routeRegex} from "app/ResolveRoute";
import {contentStats} from 'app/utils/StateFunctions' import {contentStats} from 'app/utils/StateFunctions'
import {api} from 'steem'; import {api} from 'steem';
const sagaMiddleware = createSagaMiddleware( const sagaMiddleware = createSagaMiddleware(
...@@ -59,7 +58,7 @@ const onRouterError = (error) => { ...@@ -59,7 +58,7 @@ const onRouterError = (error) => {
console.error('onRouterError', error); console.error('onRouterError', error);
}; };
async function universalRender({ location, initial_state, offchain, ErrorPage, tarantool }) { async function universalRender({ location, initial_state, offchain, ErrorPage, tarantool, redis }) {
let error, redirect, renderProps; let error, redirect, renderProps;
try { try {
[error, redirect, renderProps] = await runRouter(location, RootRoute); [error, redirect, renderProps] = await runRouter(location, RootRoute);
...@@ -119,7 +118,14 @@ async function universalRender({ location, initial_state, offchain, ErrorPage, t ...@@ -119,7 +118,14 @@ async function universalRender({ location, initial_state, offchain, ErrorPage, t
if (url.indexOf('/curation-rewards') !== -1) url = url.replace(/\/curation-rewards$/, '/transfers'); if (url.indexOf('/curation-rewards') !== -1) url = url.replace(/\/curation-rewards$/, '/transfers');
if (url.indexOf('/author-rewards') !== -1) url = url.replace(/\/author-rewards$/, '/transfers'); if (url.indexOf('/author-rewards') !== -1) url = url.replace(/\/author-rewards$/, '/transfers');
onchain = await api.getStateAsync(url); try {
onchain = await redis.get(url);
} catch (error) {
}
if (!onchain) {
onchain = await api.getStateAsync(url);
redis.set(url, onchain);
}
if (Object.getOwnPropertyNames(onchain.accounts).length === 0 && (url.match(routeRegex.UserProfile1) || url.match(routeRegex.UserProfile3))) { // protect for invalid account if (Object.getOwnPropertyNames(onchain.accounts).length === 0 && (url.match(routeRegex.UserProfile1) || url.match(routeRegex.UserProfile3))) { // protect for invalid account
return { return {
...@@ -141,7 +147,16 @@ async function universalRender({ location, initial_state, offchain, ErrorPage, t ...@@ -141,7 +147,16 @@ async function universalRender({ location, initial_state, offchain, ErrorPage, t
if (!url.match(routeRegex.PostsIndex) && !url.match(routeRegex.UserProfile1) && !url.match(routeRegex.UserProfile2) && url.match(routeRegex.PostNoCategory)) { if (!url.match(routeRegex.PostsIndex) && !url.match(routeRegex.UserProfile1) && !url.match(routeRegex.UserProfile2) && url.match(routeRegex.PostNoCategory)) {
const params = url.substr(2, url.length - 1).split("/"); const params = url.substr(2, url.length - 1).split("/");
const content = await api.getContentAsync(params[0], params[1]); const content_key = `content/${params[0]}/${params[1]}`;
let content;
try {
content = await redis.get(content_key);
} catch (error) {
}
if (!content) {
content = await api.getContentAsync(params[0], params[1]);
redis.set(content_key, content);
}
if (content.author && content.permlink) { // valid short post url if (content.author && content.permlink) { // valid short post url
onchain.content[url.substr(2, url.length - 1)] = content; onchain.content[url.substr(2, url.length - 1)] = content;
} else { // protect on invalid user pages (i.e /user/transferss) } else { // protect on invalid user pages (i.e /user/transferss)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment