Skip to content

Commit 5d545f0

Browse files
authored
Merge pull request #16 from adifiore/redis
Redis support with async implementation
2 parents 4fa169d + 990f41b commit 5d545f0

File tree

8 files changed

+66
-42
lines changed

8 files changed

+66
-42
lines changed

SSRtest/ModifiedReact.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var checkPropTypes = require('prop-types/checkPropTypes');
2727
var camelizeStyleName = require('fbjs/lib/camelizeStyleName');
2828
var stream = require('stream');
2929
var lru = require('lru-cache');
30+
var {promisify} = require('util');
3031

3132
/**
3233
* WARNING: DO NOT manually require this module.
@@ -2204,7 +2205,11 @@ var ReactDOMServerRenderer = function () {
22042205
// TODO: type this more strictly:
22052206

22062207

2207-
ReactDOMServerRenderer.prototype.read = function read(bytes, cache) {
2208+
ReactDOMServerRenderer.prototype.read = async function read(bytes, cache) {
2209+
// Promisify the get method, which is asynchronous for Redis
2210+
const getAsync = promisify(cache.get).bind(cache);
2211+
let continueLoop = true;
2212+
// Store starting locations of about-to-be-cached HTML within 'out'
22082213
const start = {};
22092214
if (this.exhausted) {
22102215
return null;
@@ -2228,21 +2233,25 @@ var ReactDOMServerRenderer = function () {
22282233
}
22292234
continue;
22302235
}
2231-
22322236
var child = frame.children[frame.childIndex++];
22332237
{
22342238
setCurrentDebugStack(this.stack);
22352239
}
22362240

2237-
// IF THE CHILD HAS A CACHEKEY PROPERTY ON IT
2238-
if(child.props.cache){
2241+
// CACHING LOGIC: EXECUTES IF THE CHILD HAS A 'CACHE' PROP ON IT
2242+
if(child.props && child.props.cache){
2243+
// Create unique cacheKey as a function of component name and props
22392244
const cacheKey = child.type.name + JSON.stringify(child.props);
2240-
if (!cache.storage.get(cacheKey)){
2245+
2246+
// Check cache (async function)
2247+
const reply = await getAsync(cacheKey);
2248+
if(reply){
2249+
out += reply;
2250+
} else {
22412251
start[cacheKey] = out.length;
22422252
out += this.render(child, frame.context, frame.domNamespace);
2243-
} else {
2244-
out += cache.storage.get(cacheKey);
22452253
}
2254+
22462255
} else {
22472256
out += this.render(child, frame.context, frame.domNamespace);
22482257
}
@@ -2252,13 +2261,14 @@ var ReactDOMServerRenderer = function () {
22522261
}
22532262
}
22542263

2255-
for (let component in start) {
2264+
// Add newly marked items to the cache:
2265+
for (let cacheKey in start) {
22562266
let tagStack = [];
22572267
let tagStart;
22582268
let tagEnd;
22592269

22602270
do {
2261-
if (!tagStart) tagStart = start[component];
2271+
if (!tagStart) tagStart = start[cacheKey];
22622272
else tagStart = (out[tagEnd] === '<') ? tagEnd : out.indexOf('<', tagEnd)
22632273
tagEnd = out.indexOf('>', tagStart) + 1;
22642274
// Skip stack logic for void/self-closing elements
@@ -2270,7 +2280,7 @@ var ReactDOMServerRenderer = function () {
22702280
} while (tagStack.length !== 0);
22712281

22722282
// cache component by slicing 'out'
2273-
cache.storage.set(component, out.slice(start[component], tagEnd));
2283+
cache.set(cacheKey, out.slice(start[cacheKey], tagEnd));
22742284
}
22752285
return out;
22762286
};
@@ -2529,9 +2539,9 @@ var ReactDOMServerRenderer = function () {
25292539
* server.
25302540
* See https://reactjs.org/docs/react-dom-server.html#rendertostring
25312541
*/
2532-
function renderToString(element, cache) {
2542+
async function renderToString(element, cache) {
25332543
var renderer = new ReactDOMServerRenderer(element, false);
2534-
var markup = renderer.read(Infinity, cache);
2544+
var markup = await renderer.read(Infinity,cache);
25352545
return markup;
25362546
}
25372547

@@ -2613,8 +2623,17 @@ class ComponentCache {
26132623
length: (n, key) => {
26142624
return n.length + key.length;
26152625
}
2616-
});
2626+
});
2627+
}
2628+
2629+
get(cacheKey, cb) {
2630+
let reply = this.storage.get(cacheKey);
2631+
// return reply;
2632+
cb(null,reply);
2633+
}
26172634

2635+
set(cacheKey, html) {
2636+
this.storage.set(cacheKey, html);
26182637
}
26192638
}
26202639

SSRtest/package-lock.json

Lines changed: 25 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SSRtest/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"react": "^16.2.0",
3535
"react-dom": "^16.2.0",
3636
"react-universal-component": "^2.5.5",
37+
"redis": "^2.8.0",
3738
"webpack-flush-chunks": "^1.1.23",
3839
"winston": "^2.3.1"
3940
},

SSRtest/src/server/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ import App from '../shared/App';
99

1010
// can pass in max-size, otherwise defaults to 1 million
1111
const cache = new ReactCC.ComponentCache();
12+
// import redis from 'redis';
13+
// const cache = redis.createClient();
1214

1315
/**
1416
* @param clientStats Parameter passed by hot server middleware
1517
*/
1618
export default ({ clientStats }) => async (req, res) => {
1719
const app = <App />;
1820
const start_cached = process.hrtime();
19-
// const appString = ReactCC.renderToStaticMarkup(app, cache);
20-
const appString = ReactCC.renderToString(app, cache);
21+
22+
23+
const appString = await ReactCC.renderToString(app, cache);
2124
const end_cached = process.hrtime(start_cached);
2225
console.info(
2326
"Cached render time: %ds %dms",

SSRtest/src/shared/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class App extends Component {
1919
<h1>THIS IS AN APP</h1>
2020
<Button />
2121
<List cache/>
22-
<BlogPost />
22+
<BlogPost thing='haha'/>
2323
</div>
2424
);
2525
}

SSRtest/src/shared/BlogPost.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default class BlogPost extends Component {
1313
<div>
1414
<h1>This is a great blog post!</h1>
1515
<BlogContent />
16+
<p>{this.props.thing}</p>
1617
</div>
1718
);
1819
}

SSRtest/src/shared/List.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ export default class List extends Component {
1616
<div>
1717
<h1>Here's my list</h1>
1818
{bunchOfPics}
19-
{/* <ListItem />
20-
<ListItem />
21-
<ListItem /> */}
2219
</div>
2320
);
2421
}

npmPackage/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

33
if (process.env.NODE_ENV === 'production') {
4-
// NOTE: CHANGE TO PRODUCTION WHEN WE HAVE THAT FINAL FILE
5-
module.exports = require('./development.js');
4+
module.exports = require('./production.js');
65
} else {
76
module.exports = require('./development.js');
87
}

0 commit comments

Comments
 (0)